![](/img/trans.png)
[英]How to change the font of particular text in a paragraph when writing a word document using Python-docx?
[英]How to increment paragraph object in word document using python-docx?
我正在搜索Word文档以获取对文档中编写的内容的描述。 但是,这些文档的格式并非全部相同。 但一致的一件事是,我想要的文本块始终位于标题“描述”之后。 因此,我将搜索“描述”,然后希望在其后获取下一个段落对象的文本。 我如何增加段落对象(可以这么说)?
for subdir, dirs, files in os.walk(rootdir):
for file in files:
doc = docx.Document(os.path.join(rootdir, file))
for paragraph in doc.paragraphs:
if 'Description' in paragraph.text:
print(paragraph[i+1].text) #I know you can't do i+1 but
#that's essentially what I want to do
一种简单的方法是:
paragraphs = list(doc.paragraphs)
for i in range(len(paragraphs)):
paragraph = paragraphs[i]
if 'Description' in paragraph.text:
print(paragraphs[i+1].text)
如果您确定说明标签出现在Heading 1
的段落中,则可以进一步限定标题段落,以免在恰好使用该词的段落中出现误报。
如果您希望提取文本并以这种方式进行搜索,则python-docx2txt会减轻您的麻烦。 它改编自python-docx。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.