如何使用python docx在标题和页脚Word文档中修改现有文本
嗨,想知道是否可以使用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.
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它已经存在于
python-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):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.