python - 是否有读取 ms office 文件的底层 xml 的 python 包?
问题描述
我想在 python 中读取 PPTX 文件的 XML,基本上将字符串/数据结构保存到变量中。
我还没有找到可以让我用 Python 做到这一点的包。
解决方案
如果我理解正确,您可以使用内置zipfile
模块。
import zipfile
archive = zipfile.ZipFile('<My Powerpoint Name>.pptx', 'r')
xml_file = archive.open('[Content_Types].xml')
text = xml_file.read()
print(text)
这将[Content_Types].xml
直接从存档中打印出 xml 文本。
如果要解析 XML,可以使用内置xml
模块。
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)
推荐阅读
- json - 在 PostgreSQL 中是否有一种自动方法将 JSON(B) 列拆分为多个列?
- typescript - 检查图像 URL 是否有效
- javascript - 如何检测/识别/查找网页上的元素
- kotlin - 为 gradle 多项目配置 TeamCity
- record - 如何使用uipath从谷歌表格中提取数据
- html - 在 CSS 中在伪元素之前使用羽毛图标
- r - R:叠加时间序列
- algorithmic-trading - 如何判断 3 条移动平均线何时在 4 根蜡烛或更少的时间内交叉
- c# - 禁止基类属性验证以支持派生类覆盖的属性验证
- grep - grep:管道进入 grep -r 无法正常工作