0

Требуется найти, есть ли совпадения нескольких словосочетаний (регулярных выражений) в таблице в файле docx, и вывести все совпадения на экран.

Вот какой код я пишу:

import re

from docx import Document

document = Document('Altai.docx')
reg542 = [re.compile("(?i).....name one.*"),
               re.compile("(?i).....name two.*")]


for table in document.tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                if re.match(reg542, paragraph.text):
                    print (paragraph.text)

Пишет, что ошибка

TypeError: unhashable type: 'list'

2 ответа 2

0

Попробуйте так:

reg542 = re.compile("(?i).....name one.*|(?i).....name two.*")

Если таких выражений будет много, то можно их через '|'.join() соединить из списка, если так будет удобнее.

1
  • Ругается на соединенный список через | SyntaxError: Non-UTF-8 code starting with '\xd0' in file /Users/pal/PycharmProjects/Finding words/main.py on line 46, but no encoding declared
    – Pavel
    29 июл 2021 в 13:30
0

This one worked

import re

from docx import Document

document = Document('file.docx')

reg542 = re.compile(r"(?i)regex 1.*"
                    r"|(?i)regex 2.*"
                    r"|(?i)regex n.*")

for table in document.tables:
    for row in table.rows:
        for cell in row.cells:
            for paragraph in cell.paragraphs:
                if reg542.search(paragraph.text):
                    print(paragraph.text)

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.