在Python中处理Word文档时,对文档属性进行操作是一项重要的任务。文档属性主要分为内置属性(如标题、作者等)和自定义属性(用户根据自身需求定义的属性)。合理地管理这些属性,能够提升文档管理效率、优化信息检索功能,并确保文档数据的准确性与安全性。本文将介绍如何使用Python实现对Word文档属性的添加、读取和删除操作,包括内置文档属性和自定义文档属性。
文章目录
- 用Python添加文档属性到Word文档
- 用Python读取Word文档的文档属性
- 用Python移除Word文档的文档属性
本文所使用的方法需要用到免费的Free Spire.Doc for Python,PyPI:pip install spire.doc.free
。
用Python添加文档属性到Word文档
我们可以使用库中的类和方法直接从文件或流载入Word文档进行处理。添加内置属性时,利用Document.BuiltinDocumentProperties
属性访问并设置相应值;添加自定义属性则借助CustomDocumentProperties.Add()
方法。具体步骤如下:
- 导入所需模块:
Document
,String
,Int32
,DateTime
,Boolean
。 - 创建`Document 类的实例。
- 使用
Document.LoadFromFile()
方法加载Word文档。 - 对内置属性,通过
Document.BuiltinDocumentProperties
属性获取并设置相关属性值。 - 对自定义属性,通过
Document.CustomDocumentProperties
属性获取,再使用Add()
方法添加不同类型的自定义属性。 - 使用
Document.SaveToFile()
方法保存修改后的文档。
代码示例
from spire.doc import Document, String, Int32, DateTime, Boolean
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("Sample.docx")
# 添加内置属性
builtin_prop = doc.BuiltinDocumentProperties
builtin_prop.Title = "Python文档属性操作"
builtin_prop.Author = "代码开发者"
builtin_prop.Category = "技术教程"
builtin_prop.Keywords = "Python,Word,属性操作"
# 添加自定义属性
custom_prop = doc.CustomDocumentProperties
custom_prop.Add("项目ID", String("PRJ001"))
custom_prop.Add("完成进度", Int32(80))
custom_prop.Add("截止日期", DateTime(2024, 12, 31, 0, 0, 0, 0))
custom_prop.Add("是否重要", Boolean(True))
# 保存文档
doc.SaveToFile("output/添加属性后的文档.docx")
doc.Close()
结果
用Python读取Word文档的文档属性
通过BuiltinDocumentProperties
类的属性,我们也可以实现读取内置属性,而读取自定义属性则依赖CustomDocumentProperty.Name
和CustomDocumentProperty.Value
属性(或CustomDocumentProperty.ToString()
方法读取为字符串)。具体操作步骤如下:
- 导入所需模块:
Document
。 - 创建
Document
类的实例。 - 使用
Document.LoadFromFile()
方法加载已添加属性的Word文档。 - 对于内置属性,通过
Document.BuiltinDocumentProperties
属性获取并读取相关属性值。 - 对于自定义属性,通过
Document.CustomDocumentProperties
属性获取,然后遍历获取每个自定义属性的名称和值。 - 输出或进一步处理读取到的属性值。
代码示例
from spire.doc import Document
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("output/添加属性后的文档.docx")
# 读取内置属性
builtin_prop = doc.BuiltinDocumentProperties
print("内置属性:")
print(f"标题: {builtin_prop.Title}")
print(f"作者: {builtin_prop.Author}")
print(f"类别: {builtin_prop.Category}")
print(f"关键词: {builtin_prop.Keywords}")
# 读取自定义属性
custom_prop = doc.CustomDocumentProperties
print("\n自定义属性:")
for i in range(custom_prop.Count):
prop_name = custom_prop.get_Item(i).Name
prop_value = custom_prop.get_Item(i).ToString()
print(f"{prop_name}: {prop_value}")
doc.Close()
结果
用Python移除Word文档的文档属性
删除内置属性时,将其值设为None
即可;删除自定义属性则使用DocumentProperties.Remove()
方法。具体操作步骤如下:
- 导入所需模块:
Document
。 - 创建
Document
类的实例。 - 使用
Document.LoadFromFile()
方法加载需要删除属性的Word文档。 - 对于内置属性,通过
Document.BuiltinDocumentProperties
属性获取并将需要删除的属性值设为None
。 - 对于自定义属性,通过
Document.CustomDocumentProperties
属性获取,然后遍历并使用Remove()
方法删除每个自定义属性。 - 使用
Document.SaveToFile()
方法保存修改后的文档。
代码示例
from spire.doc import Document
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("output/添加属性后的文档.docx")
# 删除内置属性
builtin_prop = doc.BuiltinDocumentProperties
builtin_prop.Title = None
builtin_prop.Author = None
builtin_prop.Category = None
builtin_prop.Keywords = None
# 删除自定义属性
custom_prop = doc.CustomDocumentProperties
for i in range(custom_prop.Count - 1, -1, -1):
custom_prop.Remove(custom_prop[i].Name)
# 保存文档
doc.SaveToFile("output/删除属性后的文档.docx")
doc.Close()
本文演示了如何使用Python添加、读取和删除Word文档中的内置文档属性和自定义文档属性。