#python
#python
Вопрос:
Используя приведенный ниже код, я успешно могу динамически редактировать документ Word и заменять некоторые нужные мне слова. Как я могу добавить поддержку, чтобы она также могла находить текст Word Art, поскольку он, похоже, игнорирует это.
import re
from docx import Document
def docx_replace_regex(doc_obj, regex , replace):
for p in doc_obj.paragraphs:
if regex.search(p.text):
inline = p.runs
# Loop added to work with runs (strings with same style)
for i in range(len(inline)):
if regex.search(inline[i].text):
text = regex.sub(replace, inline[i].text)
inline[i].text = text
for table in doc_obj.tables:
for row in table.rows:
for cell in row.cells:
docx_replace_regex(cell, regex , replace)
filename = "C:\Users\John\Desktop\CAR PRIVATE7.docx"
regex1 = re.compile(r"OLD")
replace1 = r"JOHN KAITA"
regex2 = re.compile(r"KBQ648E")
replace2 = r"KLM789J"
doc = Document(filename)
docx_replace_regex(doc, regex1 , replace1)
docx_replace_regex(doc, regex2 , replace2)
doc.save('C:\Users\John\Desktop\CAR PRIVATE8.docx')