Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在.docx文档中编辑Word艺术文本_Python - Fatal编程技术网

Python 如何在.docx文档中编辑Word艺术文本

Python 如何在.docx文档中编辑Word艺术文本,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.

使用下面的代码,我能够成功地动态编辑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')