如何使用python docx在标题和页脚Word文档中修改现有文本

发布于 2025-02-03 04:34:36 字数 625 浏览 6 评论 0原文

嗨,想知道是否可以使用Python docx在python的Word Doc中的标题和页脚中修改现有文本。

示例我要寻找的东西。

我希望能够编写一些可以在标题或页脚中找到文本的代码 例如,它将是“ 2020年12月”,并能够将其编辑为“ 2021年1月

from docx import Document

doc=Document('./word.docx')
Dictionary = {"2020": "2021", "December":"January"
for i in Dictionary:
    for p in doc.paragraphs:
        if p.text.find(i)>=0:
           p.text=p.text.replace(i,Dictionary[i])
            
doc.save('./word_doc2.docx')

Hi wonder if it possible to modify existing text within header and footer within word doc in python, using python docx.

Example on what I'm looking for.

enter image description here

I want to be able to write some code that can find text within the header or footer
It will be 'December 2020' for example and to be able to edit it to 'January 2021'

What has been done so far

from docx import Document

doc=Document('./word.docx')
Dictionary = {"2020": "2021", "December":"January"
for i in Dictionary:
    for p in doc.paragraphs:
        if p.text.find(i)>=0:
           p.text=p.text.replace(i,Dictionary[i])
            
doc.save('./word_doc2.docx')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2025-02-10 04:34:36

它已经存在于python-docx库的文档中,您可以找到在这里。否则,这是一个简单的脚本,可以执行您想要的操作(为您的需求进行调整):

from docx import Document
from docx.shared import Pt

# read  doc
doc = Document('./word.docx')

# define your style font and size for the header 
style_h = doc.styles['Header']
font_h = style_h.font
font_h.name = 'Arial'
font_h.bold = True
font_h.size = Pt(20)

# define your style font and size for the footer
style_f = doc.styles['Footer']
font_f = style_f.font
font_f.name = 'Aharoni'
font_f.bold = False
font_f.size = Pt(10)

# for the header:
header = doc.sections[0].header
paragraph_h = header.paragraphs[0]
paragraph_h.text = 'January 2021' # insert new value here.
paragraph_h.style = doc.styles['Header'] # this is what changes the style

# for the footer:
footer = doc.sections[0].footer
paragraph_f = footer.paragraphs[0]
paragraph_f.text = 'January 2021' # insert new value here.
paragraph_f.style = doc.styles['Footer']# this is what changes the style

doc.save('./word_doc2.docx')

nb:此仅在一个页文档上工作,如果您想将同一内容应用于所有页面(或使用docx库中的预设功能。

It is already present in the documentation of the python-docx library, you can find it Here. Otherwise, here is a simple script that does what you want (adapt it for your need):

from docx import Document
from docx.shared import Pt

# read  doc
doc = Document('./word.docx')

# define your style font and size for the header 
style_h = doc.styles['Header']
font_h = style_h.font
font_h.name = 'Arial'
font_h.bold = True
font_h.size = Pt(20)

# define your style font and size for the footer
style_f = doc.styles['Footer']
font_f = style_f.font
font_f.name = 'Aharoni'
font_f.bold = False
font_f.size = Pt(10)

# for the header:
header = doc.sections[0].header
paragraph_h = header.paragraphs[0]
paragraph_h.text = 'January 2021' # insert new value here.
paragraph_h.style = doc.styles['Header'] # this is what changes the style

# for the footer:
footer = doc.sections[0].footer
paragraph_f = footer.paragraphs[0]
paragraph_f.text = 'January 2021' # insert new value here.
paragraph_f.style = doc.styles['Footer']# this is what changes the style

doc.save('./word_doc2.docx')

N.B: This work on only one page docs, you can easily add a loop if you want to apply the same thing to all the pages (or use pre-set functions from the docx library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文