小编har*_*sha的帖子

突出显示python中的文本并将其保存在word文件中

我正在尝试从 word 文件中获取文本并突出显示所需的文本,并且老化想要将文本保存到新的 word 文件中。

我能够使用 ANSI 转义序列突出显示文本,但我无法将其添加回 word 文件。

from docx import Document
doc = Document('t.docx')
##string present in t.docx '''gnjdkgdf helloworld dnvjk dsfgdzfh jsdfKSf klasdfdf sdfvgzjcv'''

if 'helloworld' in doc.paragraphs[0].text:    
    high=doc.paragraphs[0].text.replace('helloworld', '\033[43m{}\033[m'.format('helloworld'))


doc.add_paragraph(high)
doc.save('t1.docx')
Run Code Online (Sandbox Code Playgroud)

得到这个错误。

ValueError: All strings must be XML compatible: Unicode or ASCII, no NULL bytes or control characters
Run Code Online (Sandbox Code Playgroud)

python-3.x python-docx

5
推荐指数
1
解决办法
3937
查看次数

标签 统计

python-3.x ×1

python-docx ×1