【问题标题】:Remove all metadata from powerpoint presentation using python-pptx使用 python-pptx 从 powerpoint 演示文稿中删除所有元数据
【发布时间】:2019-07-26 14:03:33
【问题描述】:

我可以使用以下代码删除/覆盖一些元数据(存储在 core.xml 中):

def remove_metadata(prs):
    """Overwrites the metadata in core.xml however does not overwrite metadata which is stored in app.xml"""
    prs.core_properties.title = 'PowerPoint Presentation'
    prs.core_properties.last_modified_by = 'python-pptx'
    prs.core_properties.revision = 1
    prs.core_properties.modified = datetime.utcnow()
    prs.core_properties.subject = ''
    prs.core_properties.author = 'python-pptx'
    prs.core_properties.keywords = ''
    prs.core_properties.comments = ''
    prs.core_properties.created = datetime.utcnow()
    prs.core_properties.category = ''

prs = pptx.Presentation('my_pres.xml')
remove_metadata(prs)

这很有用 - 但还有其他元数据存储在 app.xml 中,例如 Company 和 Manager。我还需要清除这些属性。使用 python-pptx 如何编辑 app.xml 文件?

【问题讨论】:

    标签: python-pptx


    【解决方案1】:

    我找到了解决方案。这不一定是处理此问题的理想方法,但似乎可行:

    def remove_metadata_from_app_xml(prs):
        """There is currently no functionality for handling app.xml so 
        have to find the part and then alter its blob manually
        """
        package_parts = prs.part.package.parts
        for part in package_parts:
            if part.partname.endswith('app.xml'):
                app_xml_part = part
        app_xml = app_xml_part.blob.decode('utf-8')
        tags_to_remove = ('Company', 'Manager', 'HyperlinkBase')
        for tag in tags_to_remove:
            pattern = f'<{tag}>.*<\/{tag}>'
            app_xml = re.sub(pattern, '', app_xml)
        app_xml_part.blob = bytearray(app_xml, 'utf-8')
    

    【讨论】:

      猜你喜欢
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多