python-docx:如何将文档属性获取到对象中

问题描述 投票:0回答:1

我正在使用 python-docx,我可以用

doc.core_properties.__dir__()
列出属性,如本问题所示。 但是将 prs.core_properties 分配给 python 变量并不会给出可用的对象。

如何将文档属性获取到Python对象中?

这是我尝试过的:

from pptx import Presentation
import os

pptFile = "myfile.pptx"
prs = Presentation(os.path.realpath(pptFile))
prsProps = prs.core_properties
print(prs.core_properties.__dir__())
print(prs.core_properties.modified)
prsProps

产量

['_partname', '_content_type', '_package', '_blob', '_element', '_rels', '__module__', '__doc__', 'default', 'author', 'category', 'comments', 'content_status', 'created', 'identifier', 'keywords', 'language', 'last_modified_by', 'last_printed', 'modified', 'revision', 'subject', 'title', 'version', '_new', '__init__', 'load', 'blob', 'part', 'content_type', 'drop_rel', 'load_rels_from_xml', 'package', 'partname', 'rels', '_blob_from_file', '_rel_ref_count', 'part_related_by', 'relate_to', 'related_part', 'target_ref', '__dict__', '__weakref__', '__new__', '__repr__', '__hash__', '__str__', '__getattribute__', '__setattr__', '__delattr__', '__lt__', '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__reduce_ex__', '__reduce__', '__getstate__', '__subclasshook__', '__init_subclass__', '__format__', '__sizeof__', '__dir__', '__class__']
2019-11-15 12:30:37
<pptx.parts.coreprops.CorePropertiesPart at 0x1d9a3fa6fd0>

我期待

prsProps
中有一个物体。

python python-docx python-pptx
1个回答
0
投票

要获取 docx 核心属性,您只需在文档对象上访问它:

>>> core_properties = document.core_properties
>>> core_properties
<docx.opc.coreprops.CoreProperties at 0x...>

CoreProperties
是一个具有 14 个读/写属性的对象,记录如下:

https://python-docx.readthedocs.io/en/latest/api/document.html#coreproperties-objects

© www.soinside.com 2019 - 2024. All rights reserved.