python-如何从docx中删除属性

时间:2018-06-28 07:51:32

标签: python xml docx

我需要从组织中的.docx文件中删除一些元数据,从中传出的docs文件应该没有任何属性,例如创建者,时间,修改 我正在尝试使用python xml树来处理它,(我首先使用docx api尝试了它并不能正常工作..):

docx_path = 'C:\\Users\\user\\Downloads\\demo.docx'
directory_to_extract_to = 'C:\\Users\\user\\Downloads\\unzipped'

def unzip_docx_file():
    file = zipfile.ZipFile(docx_path)
    file.extractall(directory_to_extract_to)
    file.close()

def read_xml_tags():
    tree = ET.parse(directory_to_extract_to + '\\docProps\\core.xml')
    root = tree.getroot()

    for child in root:
        child.text = '**'
        print(child.tag , ' ; ' , child.text)

    #import pdb
    #pdb.set_trace()    

if __name__ == '__main__':
    #unzip_docx_file()
    read_xml_tags()
    print ('End of program')

现在这件事在内存中起作用了,我如何将这些更改保存到docProps目录中的实际core.xml文件中?

0 个答案:

没有答案