【问题标题】:Set background color (shading) on normal text with python-docx使用 python-docx 在普通文本上设置背景颜色(阴影)
【发布时间】:2020-08-31 06:11:21
【问题描述】:

有很多用于在表格中设置背景颜色的 SO 线程,但我想更改段落中一段普通文本的颜色。

我可以设置高亮颜色,但我只能选择 17 种可用颜色中的一种。

在 Word 中,背景颜色称为“阴影”,通过单击油漆桶图标完成。

虽然突出显示只会改变文本后面的颜色,但阴影会绘制整行的颜色 - 无论哪种方式我都可以。

是否可以使用 python-docx 设置不在表格中的文本后面的任何 RGB 值?

【问题讨论】:

  • 可能包含示例图像。我不确定“整体的颜色”是什么意思。你说的是文本的大纲吗?

标签: python ms-word python-docx


【解决方案1】:

如果要对整行应用底纹,而不仅仅是文本下方的区域,则需要将底纹元素应用于段落样式而不是行。基于@Frankie 的解决方案:

    from docx import Document
    from docx.shared import Pt
    from docx.oxml.ns import qn
    from docx.oxml.shared import OxmlElement

    # Create a template
    doc = Document()

    # Add a paragraph
    p = doc.add_paragraph('Custom background colour (a.k.a shading, '
                          'done with the paint bucket tool)')

    # Create XML element
    shd = OxmlElement('w:shd')

    # Add attributes to the element
    shd.set(qn('w:val'), 'clear')
    shd.set(qn('w:color'), 'auto')
    shd.set(qn('w:fill'), 'FFAAAA')

    # Make sure the paragraph styling element exists
    p.paragraph_format.element.get_or_add_pPr()

    # Append the shading element
    p.paragraph_format.element.pPr.append(shd)

    # Save document
    doc.save('d.docx')

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,但我找到了一个技巧来做你所要求的。

    首先需要导入这三个模块:

    from docx.shared import Pt
    from docx.oxml.ns import qn
    from docx.oxml.shared import OxmlElement
    

    这些模块是必不可少的,因为您需要访问运行对象的“XML 版本”,并在<w:rPr> 下添加一个用于设置阴影的<w:shd> 元素。

    # Create a template
    doc = Document()
    
    # Add a paragraph
    p = doc.add_paragraph()
    
    # Add text to paragraph reference
    txt = 'Custom background colour (a.k.a shading, done with the paint bucket tool)'
    run = p.add_run(txt)
        
    # Get the XML tag
    tag = run._r
    print(run.element.xml) # print XML
    
    # Create XML element
    shd = OxmlElement('w:shd')
    
    # Add attributes to the element
    shd.set(qn('w:val'), 'clear')
    shd.set(qn('w:color'), 'auto')
    shd.set(qn('w:fill'), 'FFAAAA')
    

    在打印输出命令中,会输出:

    <w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
        <w:t>Custom background colour (a.k.a shading, done with the paint bucket tool)</w:t>
    </w:r>
    

    但是仍然没有&lt;w:rPr&gt; 元素。此时,您可以像上面那样创建另一个 XML 元素,但我做了以下操作:

    # This is the tricky part
    run.font.size = Pt(14)
    print(run.element.xml)
    

    第二条打印输出命令后,会输出:

    <w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
        <w:rPr>
            <w:sz w:val="28"/>
        </w:rPr>
        <w:t>Custom background colour (a.k.a shading, done with the paint bucket tool)</w:t>
    </w:r>
    

    最后,需要将元素追加到&lt;w:rPr&gt;

    tag.rPr.append(shd)
    print(run.element.xml)
    

    这是最后的打印命令:

    <w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
        <w:rPr>
            <w:sz w:val="28"/>
            <w:shd w:val="clear" w:color="auto" w:fill="FFAAAA"/>
        </w:rPr>
        <w:t>Custom background colour (a.k.a shading, done with the paint bucket tool)</w:t>
    </w:r>
    

    当然,您需要将输出保存到文件中:

    # Save document
    doc.save('d.docx')
    

    把所有东西放在一起:

    from docx import Document
    from docx.shared import Pt
    from docx.oxml.ns import qn
    from docx.oxml.shared import OxmlElement
    
    # Create a template
    doc = Document()
    
    # Add a paragraph
    p = doc.add_paragraph()
    
    # Add text to paragraph reference
    txt = 'Custom background colour (a.k.a shading, done with the paint bucket tool)'
    run = p.add_run(txt)
    
    # Get the XML tag
    tag = run._r
    print(run.element.xml)
    
    # Create XML element
    shd = OxmlElement('w:shd')
    
    # Add attributes to the element
    shd.set(qn('w:val'), 'clear')
    shd.set(qn('w:color'), 'auto')
    shd.set(qn('w:fill'), 'FFAAAA')
    
    # Set the font size - this is important! Without this step the
    # tag.rPr value below will be None.
    run.font.size = Pt(14)
    print(run.element.xml)
    
    tag.rPr.append(shd)
    print(run.element.xml)
    
    # Save document
    doc.save('d.docx')
    

    我不知道这个方法是否适合你,希望对你有帮助。如果您想更改为其他样式而不是阴影,我认为这也应该可以解决问题。但你确实需要知道元素是什么。此外,您还可以找到有关 style 的文档,该文档提供了一个 XML 示例,以显示 Word 文档在 XML 格式中的外观。

    【讨论】:

    • 您可以使用:run.element.get_or_add_rPr(),而不是设置字体大小以确保 Tag.rPr 存在,这将导致 tag.rPr 不是 None
    猜你喜欢
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2013-07-29
    • 2021-12-03
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多