python - 通过 Python 在 *.docx 文件的标头中添加标签
问题描述
我有一个问题:我正在开发一个项目,该项目使用 Python 和python-docx在当前文件夹中的 .docx 文件的标题中自动插入文本标签。但它什么也做不了。这是代码。对一些糟糕的处决感到抱歉。我只是这个编码方面的业余爱好者。
from docx import Document
import os
path = os.path.dirname(os.path.realpath('__file__'))
print(path)
def label(docFileName):
document = Document(docFileName)
section = document.sections[0]
header = section.header
header
header.is_linked_to_previous = True
header = document.sections[0].header
paragraph = header.paragraphs[0]
paragraph.text = "Left Header\t[CONFIDENTIAL]\tRight Header"
paragraph.style = document.styles["Normal"]
return print("Labeled")
for file in os.listdir():
if file.endswith(".docx"):
docFileName = os.path.join(path,file)
print(docFileName)
label(docFileName)
解决方案
放下header.is_linked_to_previous = True
线。这基本上删除了标题。如果您有意删除现有标题,请header.is_linked_to_previous = False
在该header = document.sections[0].header
行之后设置,然后再访问其段落。
推荐阅读
- reactjs - 有没有办法在 ReactJS 中为组件使用小写字母?
- r - 我如何 pivot_wider() 以便在自己的列中维护重复项?
- java - 使用 apache poi 保护单元?
- python - 如何将此json文件转换为表?
- flutter - 如何在 Flutter 中将身份验证令牌传递给 WebView?
- go - 使用 gon 的 goreleaser 签名 - 捆绑格式无法识别、无效或不合适
- ossec - wazuh manager - wazuh-db 无法启动
- python - Python中GLM公式中的绑定列
- javascript - 如何在 keyup 事件中获取已删除的字符
- c++ - 更改lookatmatrix后OpenGL消失的元素