4

我正在使用 python 3.4 和python-docx库来处理.docx文件。我已经能够从文档中提取文本。但我的目标是只提取具有特定字体的文本(并修改它们)。

过去两天我一直在图书馆文档中搜索这个,但没有结果。

这里有没有人有这个图书馆的经验,如果有的话,他们能给我指出正确的方向。

4

1 回答 1

2

At present, python-docx only has the ability to apply a font typeface using a style. You can detect runs having a particular style like this:

document = Document('having-fonts.docx')
for paragraph in document.paragraphs:
    for run in paragraph.runs:
        if run.style == style_I_want:
            print run.text

If the special fonts are applied using a paragraph style you could use this:

document = Document('having-fonts.docx')
for paragraph in document.paragraphs:
    if paragraph.style == style_I_want:
        print paragraph.text

If you can say more about the particulars I may be able to be more specific.

于 2014-09-01T18:32:31.600 回答