Is there a python package that reads the underlying xml of ms office files?

Viewed 483

I want to read the XML of a PPTX file in python and basically save the string / data structure into a variable.

I couldn't find a package that let's me do this with Python yet.

1 Answers

If I'm understanding correctly, you can just use the builtin zipfile module.

import zipfile
archive = zipfile.ZipFile('<My Powerpoint Name>.pptx', 'r')
xml_file = archive.open('[Content_Types].xml')
text = xml_file.read()
print(text)

This will print out the xml text from the [Content_Types].xml directly inside the archive.

If you want to parse the XML you can use the builtin xml module.

import zipfile
import xml.etree.ElementTree as ET

archive = zipfile.ZipFile('<My Powerpoint Name>.pptx', 'r')
xml_file = archive.open('[Content_Types].xml')
text = xml_file.read()

root = ET.fromstring(text)
value_to_find = r'application/vnd.openxmlformats-package.relationships+xml'
for child in root:
    if child.attrib['ContentType'] == value_to_find:
        print(child.attrib)
Related