Python docx在保持样式的同时替换段落中的字符串 [英] Python docx Replace string in paragraph while keeping style

查看:76
本文介绍了Python docx在保持样式的同时替换段落中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助替换 Word 文档中的字符串,同时保持整个文档的格式.

I need help replacing a string in a word document while keeping the formatting of the entire document.

我正在使用 python-docx,在阅读文档后,它适用于整个段落,所以我松散了粗体或斜体字等格式.包括要替换的文本以粗体显示,我想保持这种方式.我正在使用此代码:

I'm using python-docx, after reading the documentation, it works with entire paragraphs, so I loose formatting like words that are in bold or italics. Including the text to replace is in bold, and I would like to keep it that way. I'm using this code:

from docx import Document
def replace_string2(filename):
    doc = Document(filename)
    for p in doc.paragraphs:
        if 'Text to find and replace' in p.text:
            print 'SEARCH FOUND!!'
            text = p.text.replace('Text to find and replace', 'new text')
            style = p.style
            p.text = text
            p.style = style
    # doc.save(filename)
    doc.save('test.docx')
    return 1

因此,如果我实现它并想要类似的东西(包含要替换的字符串的段落丢失其格式):

So if I implement it and want something like (the paragraph containing the string to be replaced loses its formatting):

这是段落 1,这是一段粗体的文字.

This is paragraph 1, and this is a text in bold.

这是第2段,我将替换旧文本

当前结果是:

这是段落 1,这是一段粗体的文字.

This is paragraph 1, and this is a text in bold.

这是第 2 段,我将替换新文本

This is paragraph 2, and I will replace new text

推荐答案

我发布了这个问题(尽管我在这里看到了一些相同的问题),因为这些(据我所知)都没有解决这个问题.有一个使用 odocx 库,我尝试过,但没有用.所以我找到了一个解决方法.

I posted this question (even though I saw a few identical ones on here), because none of those (to my knowledge) solved the issue. There was one using a oodocx library, which I tried, but did not work. So I found a workaround.

代码非常相似,但逻辑是:当我找到包含我想要替换的字符串的段落时,使用runs添加另一个循环.(这仅在我希望替换的字符串具有相同格式时才有效).

The code is very similar, but the logic is: when I find the paragraph that contains the string I wish to replace, add another loop using runs. (this will only work if the string I wish to replace has the same formatting).

def replace_string(filename):
    doc = Document(filename)
    for p in doc.paragraphs:
        if 'old text' in p.text:
            inline = p.runs
            # Loop added to work with runs (strings with same style)
            for i in range(len(inline)):
                if 'old text' in inline[i].text:
                    text = inline[i].text.replace('old text', 'new text')
                    inline[i].text = text
            print p.text

    doc.save('dest1.docx')
    return 1

这篇关于Python docx在保持样式的同时替换段落中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆