更改段落中单个单词的颜色,python-docx

0x6upsns  于 2022-12-20  发布在  Python
关注(0)|答案(1)|浏览(138)

我正在读取一个 * docx * 文件,文件名为python-docx,我正在对段落文本进行一些更改,因此,每次更改文本时,我都会丢失一些单词的颜色:

下面是我的代码:

def get_paragraphs(self, doc, paragraphs = []):
    for p in doc.paragraphs:
        if p.text:
            if p.text[0] == r'{':
                continue 
            if p.text.isspace():
                continue

            p.text = p.text.replace("Before", "After")
            paragraphs.append(p.text)

    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                self.get_paragraphs(cell, paragraphs)

    if not doc._parent:
        return paragraphs

我想我可以在文本中添加一个标签,比如"\<red\>Red\<red\>",然后以段落样式传递它,但是我不知道该怎么做?

s8vozzvw

s8vozzvw1#

使用paragraph对象的“runs”列表,我可以检查每个属性,比如颜色,文本是粗体还是斜体,然后创建一个“html标签”,在那里我可以使用BeatifullSoup获得这些参数:

def get_paragraphs(doc, paragraphs = []):
for p in doc.paragraphs:
    if p.text:
        if p.text[0] == r'{':
            continue 
        if p.text.isspace():
            continue
        
        
        runs_text = []
        for r in p.runs:
            text_tag = f'<text color="{r.font.color.rgb}">{r.text}</text>'.replace('Before', 'After')                
            runs_text.append(text_tag)
        
        p.text = ''

        # turn html to paragraphs
        for text_tag in runs_text:
            tag = BeautifulSoup(text_tag).find('text')
            text = tag.text

            run = p.add_run(text)
            color = tuple(int(tag.get('color')[i:i+2], 16)  for i in (0, 2, 4))
            run.font.color.rgb = RGBColor(*color)

        paragraphs.append(p.text)
        
for table in doc.tables:
    for row in table.rows:
        for cell in row.cells:
            get_paragraphs(cell, paragraphs)

if not doc._parent:
    return paragraphs

相关问题