本主题介绍如何使用 Python 显示 Excel 中的文档属性。它包含配置 IDE 以使用此功能的详细信息、完成任务的编程步骤列表以及使用 Python 显示 **Excel 文件属性的示例代码。您还将了解到如何使用不同的参数作为参数来访问内置和自定义属性。
使用 Python 在 Excel 中显示文档属性的步骤
- 将 IDE 设置为使用 Aspose.Cells for Python via Java 读取属性
- 在 Workbook 类中打开源 Excel 文件以显示属性
- 迭代工作簿中的所有自定义文档属性,并在输出控制台中显示属性名称和值
- 循环遍历所有 builtin properties 并在输出控制台中显示名称和值
- 使用属性名称或索引访问各种其他属性
上述步骤简要说明了如何使用Python在Excel中显示文档属性。访问源 Excel 文件,循环遍历所有自定义/内置属性,并输出一些属性,例如名称和值。根据要求使用属性名称或索引访问各个文档属性。
使用 Python 显示 Excel 文档属性的代码
import jpype | |
import asposecells | |
jpype.startJVM() | |
from asposecells.api import License, Workbook, CustomDocumentPropertyCollection, DocumentProperty, BuiltInDocumentPropertyCollection | |
# Instantiate product license | |
License().setLicense("License.lic") | |
# Load source Workbook file | |
wb = Workbook("sample.xlsx") | |
for custProp in workbook.getCustomDocumentProperties(): | |
print("Workbook Custom Property Name: " + custProp.getName() + ", Value = " + custProp.getValue()) | |
for builtInProp in workbook.getBuiltInDocumentProperties(): | |
print("Workbook Builtin Property Name: " + builtInProp.getName() + ", Value = " + builtInProp.getValue()) | |
# fetch a list of all builtin document properties of the Excel file | |
builtinProperties = workbook.getBuiltInDocumentProperties() | |
# Access a builtin document property by using the property name | |
builtinProperty = builtinProperties.get("Author") | |
print(builtinProperty.getName() + " " + builtinProperty.getValue()) | |
# Access the same builtin document property by using the property index | |
builtinProperty = builtinProperties.get(0) | |
print(builtinProperty.getName() + " " + builtinProperty.getValue()) | |
print("Done") | |
jpype.shutdownJVM() |
此示例代码展示了如何使用 Python 在 Excel 中显示属性。值得一提的是,如果 Excel 文件中可用,则将显示自定义属性,但是默认情况下始终显示内置属性。要管理单个属性,您可以使用任何其他内置属性,例如 LastSavedBy、Author、CreateTime、LastSavedTime、Version 等。
本主题启发我们访问和显示 Excel 文件中的属性。要删除 Excel 文件中的分页符,请参阅有关 使用 Python 删除 Excel 中的分页符 的文章。