如何使用 Python Docx 更新 MS Word 中的字段
How to update fields in MS Word with Python Docx
我正在开发一个 Python 程序,该程序需要将 MS Word 中的标题文本添加到图形和 Table 中(带编号)。但是,添加该字段后,该字段不会出现在我的 Word-document 中,直到我更新该字段(它在我的文档中只是一个空的 space,直到我更新该字段,然后它跳转到例如 '2 ').
这是我添加字段的代码:
def add_caption_number(self, field_code):
""" Add a caption number for the field
:argument
field_code: [string] the type of field e.g. 'Figure', 'Table'...
"""
# Set the pointer to the last paragraph (e.g. the 'Figure ' caption text)
run = self.last_paragraph.add_run()
r = run._r
# Add a Figure Number field xml element
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "begin")
r.append(fldChar)
instrText = OxmlElement("w:instrText")
instrText.text = " SEQ %s \* ARABIC" % field_code
r.append(instrText)
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "end")
r.append(fldChar)
self.last_paragraph
是添加的最后一段,field_code
是select是否添加图或Table标题编号。
我找到了一个更新字段的示例,但这会在打开文档时打开以下 window:
def update_fields(save_path):
""" Automatically updates the fields when opening the word document """
namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
doc = DocxTemplate(save_path)
element_updatefields = lxml.etree.SubElement(
doc.settings.element, f"{namespace}updateFields"
)
element_updatefields.set(f"{namespace}val", "true")
doc.save(save_path)
有没有办法在没有弹出窗口 window 并且不向 Word 文档添加宏的情况下执行此操作?这需要在 MacOS 和 Windows 顺便说一句。
问题中描述的行为是设计使然。字段更新存在潜在 安全风险 - 某些字段类型可以访问外部内容。因此,在Word之外生成的动态内容UI需要用户确认才能更新。
我知道只有三种方法可以防止显示提示
计算值并在文档生成期间插入字段结果。这些字段仍将以正常方式更新,但在第一次打开文档时不需要更新。 (省略问题第二部分的代码。)
使用 Word Automation Services(需要本地 SharePoint)打开文档,这将更新字段(如问题的第二部分)。
在 AutoOpen
宏中包含一个执行字段更新的 VBA 项目。当然,这意味着文档类型必须启用宏 (docm),并且允许在目标安装上执行宏(当然,这也是一个安全风险)。
我正在开发一个 Python 程序,该程序需要将 MS Word 中的标题文本添加到图形和 Table 中(带编号)。但是,添加该字段后,该字段不会出现在我的 Word-document 中,直到我更新该字段(它在我的文档中只是一个空的 space,直到我更新该字段,然后它跳转到例如 '2 ').
这是我添加字段的代码:
def add_caption_number(self, field_code):
""" Add a caption number for the field
:argument
field_code: [string] the type of field e.g. 'Figure', 'Table'...
"""
# Set the pointer to the last paragraph (e.g. the 'Figure ' caption text)
run = self.last_paragraph.add_run()
r = run._r
# Add a Figure Number field xml element
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "begin")
r.append(fldChar)
instrText = OxmlElement("w:instrText")
instrText.text = " SEQ %s \* ARABIC" % field_code
r.append(instrText)
fldChar = OxmlElement("w:fldChar")
fldChar.set(qn("w:fldCharType"), "end")
r.append(fldChar)
self.last_paragraph
是添加的最后一段,field_code
是select是否添加图或Table标题编号。
我找到了一个更新字段的示例,但这会在打开文档时打开以下 window:
def update_fields(save_path):
""" Automatically updates the fields when opening the word document """
namespace = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
doc = DocxTemplate(save_path)
element_updatefields = lxml.etree.SubElement(
doc.settings.element, f"{namespace}updateFields"
)
element_updatefields.set(f"{namespace}val", "true")
doc.save(save_path)
有没有办法在没有弹出窗口 window 并且不向 Word 文档添加宏的情况下执行此操作?这需要在 MacOS 和 Windows 顺便说一句。
问题中描述的行为是设计使然。字段更新存在潜在 安全风险 - 某些字段类型可以访问外部内容。因此,在Word之外生成的动态内容UI需要用户确认才能更新。
我知道只有三种方法可以防止显示提示
计算值并在文档生成期间插入字段结果。这些字段仍将以正常方式更新,但在第一次打开文档时不需要更新。 (省略问题第二部分的代码。)
使用 Word Automation Services(需要本地 SharePoint)打开文档,这将更新字段(如问题的第二部分)。
在
AutoOpen
宏中包含一个执行字段更新的 VBA 项目。当然,这意味着文档类型必须启用宏 (docm),并且允许在目标安装上执行宏(当然,这也是一个安全风险)。