【问题标题】:Can we find a specific word in a docx file using python?我们可以使用 python 在 docx 文件中找到特定的单词吗?
【发布时间】:2019-06-04 09:00:16
【问题描述】:

我尝试过使用 python-docx 模块。到目前为止,我已经能够从 word 文件中提取特定段落以及整个文本。

pip install --pre python-docx #to install python-docx

from docx import Document
document = Document('file.docx')

document.paragraphs  # to extract paragraphs
document.paragraphs[2].text  # gives the text
​
for par in document.paragraphs:  # to extract the whole text
  print(par.text)


# I tried the below code to find some specific term
for i in range(0, 50, 1):
  if (document.paragraphs[i].text == ('Some-word')):
    print document.paragraph

我希望在 word 文件中以突出显示的形式找到特定的单词

【问题讨论】:

  • 使用'Some-word' in document.paragraphs[i].text
  • 我试过用它。它们中的任何一个都没有输出。

标签: python ms-word docx python-docx word-frequency


【解决方案1】:

它会搜索所有段落

for par in document.paragraphs:  # to extract the whole text
  if 'Some-word' in par.text:
     print(par.text)

【讨论】:

  • 用这种方法找到的特定单词后如何复制一些“句子”?
猜你喜欢
  • 2020-09-07
  • 2019-05-21
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2019-11-06
相关资源
最近更新 更多