Можем ли мы найти определенное слово в файле docx, используя python? - PullRequest
0 голосов
/ 04 июня 2019

Я пытался использовать модуль python-docx.До сих пор я был в состоянии извлечь отдельные абзацы, а также весь текст из файла слов.

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

Я ожидаю найти определенное слово в выделенномФорма в текстовом файле

1 Ответ

1 голос
/ 05 июня 2019

Поиск по всем абзацам

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