A
A
AinzO2021-07-29 15:52:17
Python
AinzO, 2021-07-29 15:52:17

(Python, docx) Why can't I replace a word exactly in the table?

There is this code that replaces "apple" with "apple", but the problem is that the code ignores the word "apple" if it is in the table.. How can I fix this?

import docx
 
document = docx.Document("one.docx")
 
for paragraph in document.paragraphs:
    paragraph.text = paragraph.text.replace("apple", "яблоко")

document.save("one.docx")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PNEroshevich, 2021-07-29
@AinzO

You need to pay attention to the structure of the file, in addition to paragraphs in the document, there are tables and headers and footers.

#Таблицы
document.tables
#Заголовки
document.header
#Подвалы
document.footer

Of course, tables have rows and cells.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question