自定义文档属性是 word 文档中由用户定义的字段,用于存储特定的元数据。与 microsoft word 预定义的标准属性(如标题、作者或主题)不同,自定义属性允许用户根据实际需求灵活地定义和管理额外的元数据字段。本文将介绍如何使用 spire.doc for python 和 python 在 word 文档中添加、提取和删除自定义文档属性。
安装 spire.doc for python
本教程需要 spire.doc for python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 windows 中。
pip install spire.doc
如果您不确定如何安装,请参考:如何在 windows 中安装 spire.doc for python
python 在 word 文档中添加自定义文档属性
spire.doc for python 提供了 customdocumentproperties.add() 方法,允许开发人员为 word 文档的自定义属性赋予不同类型的值,如文本(text)、日期(date)、数字(number)或“是/否”(yes or no)等。下面的步骤展示了如何使用 spire.doc for python 将不同类型的自定义属性添加到 word 文档中:
- 初始化 document 类的实例。
- 使用 document.loadfromfile() 方法加载 word 文档。
- 通过 document.customdocumentproperties 属性获取文档的自定义文档属性。
- 使用 customdocumentproperties.add(name, value) 方法向文档中添加不同数据类型的自定义文档属性。
- 使用 document.savetofile() 方法保存文档。
- python
from spire.doc import *
from spire.doc.common import *
# 创建document对象
document = document()
# 加载word文档
document.loadfromfile("示例.docx")
# 向文档中添加不同类型值的自定义文档属性
customproperties = document.customdocumentproperties
customproperties.add("文档类别", string("技术报告"))
customproperties.add("修订编号", int32(5))
customproperties.add("最后审阅日期", datetime(2024, 12, 1, 0, 0, 0, 0))
customproperties.add("是否需要跟进", boolean(false))
# 保存结果文档
document.savetofile("添加自定义文档属性.docx", fileformat.docx2016)
document.close()
python 提取 word 文档中的自定义文档属性
提取自定义文档属性可以帮助开发人员访问文档中的元数据,便于进行深入分析、生成报告或与其他应用程序集成。在 spire.doc for python 中,开发人员可以使用 customdocumentproperty.name 和 customdocumentproperty.value 属性轻松获取这些自定义属性的名称和值。以下是详细步骤:
- 初始化 document 类的实例。
- 使用 document.loadfromfile() 方法加载 word 文档。
- 通过 document.customdocumentproperties 属性获取文档的自定义文档属性。
- 遍历所有自定义文档属性。
- 使用 customdocumentproperty.name 和 customdocumentproperty.value 属性提取每个自定义文档属性的名称和值。
- 将提取的内容保存到文本文件中。
- python
from spire.doc import *
from spire.doc.common import *
# 创建document对象
document = document()
# 加载word文档
document.loadfromfile("添加自定义文档属性.docx")
# 打开文本文件保存提取的自定义属性
with open("自定义文档属性.txt", "w") as output_file:
# 遍历所有自定义文档属性
for i in range(document.customdocumentproperties.count):
# 提取每个自定义属性的名称和值
property_name = document.customdocumentproperties[i].name
property_value = document.customdocumentproperties[i].value
# 将属性详细信息写入文本文件
output_file.write(f"{property_name}: {property_value}\n")
document.close()
python 从 word 文档中删除自定义文档属性
删除自定义文档属性对于保护文档的机密性、减小文件大小以及确保元数据不包含过时或无关信息至关重要。spire.doc for python 为开发人员提供了 documentproperties.remove() 方法,用于删除指定的自定义文档属性。以下是详细步骤:
- 初始化 document 类的实例。
- 使用 document.loadfromfile() 方法加载 word 文档。
- 通过 document.customdocumentproperties 属性获取文档的自定义文档属性。
- 遍历所有自定义文档属性。
- 使用 documentproperties.remove() 方法按名称删除每个自定义文档属性。
- 使用 document.savetofile() 方法保存文档。
- python
from spire.doc import *
from spire.doc.common import *
# 创建document对象
document = document()
# 加载word文档
document.loadfromfile("添加自定义文档属性.docx")
# 遍历所有自定义文档属性
customproperties = document.customdocumentproperties
for i in range(customproperties.count - 1, -1, -1):
# 通过名称删除每个自定义文档属性
customproperties.remove(customproperties[i].name)
# 保存结果文档
document.savetofile("删除自定义文档属性.docx", fileformat.docx2016)
document.close()
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。