【发布时间】:2021-08-02 10:02:12
【问题描述】:
我一直在尝试在 Python3 中解决这个问题。
我通常使用 python-docx 库从 DOCX 文档中提取一些信息。
from docx.document import Document
from docx import Document
document = Document("test.docx")
for paragraph in document.paragraphs:
for run in paragraph.runs:
print(run.font.name)
#returns None
所以,从上面的代码可以看出,这是一个非常简单的python-docx 代码,用于提取一些信息。我可以访问一些属性,例如; font name、size、outline levels等
但是,所有这些属性都返回None。因为它们没有被明确定义。
我已经检查过 StackOverflow 是否存在类似问题并找到了这些问题。
Extracting word document with styles associated to the content
How to get actual style of text in word document using python docx
在文档中,它还说,如果它返回None,那么它就是Default 样式,即被继承。
也尝试了一些 XML 解析,但无法达到所需的参数:
words = document._element.xpath('//w:r')
WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
for elem in document.element.getiterator():
if elem.tag == WORD_NAMESPACE + 'p':
for i, child in enumerate(elem.getchildren()):
if child.tag == WORD_NAMESPACE + 'pPr':
...
# No idea how to access, all the styles with which
# tags etc.
我们如何也提取这些默认样式?我想从 DOCX 中提取缩进级别、粗体、斜体、字体名称、大小等属性。有什么替代方法。我想在 Python3 中解决它。
【问题讨论】:
标签: python-3.x xml python-docx