首页 > 解决方案 > 使用 python-docx 在普通文本上设置背景颜色(阴影)

问题描述

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

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

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

虽然突出显示只会改变文本后面的颜色,但阴影会绘制整行的颜色——无论哪种方式对我来说都很好。

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

格式化示例

标签: pythonms-wordpython-docx

解决方案


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

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

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

这些模块是必不可少的,因为您需要访问运行对象的“XML 版本”,并<w:shd><w:rPr>.

# 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>

但是仍然没有<w:rPr>元素。此时,您可以像上面那样创建另一个 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>

最后,您需要将元素附加到<w:rPr>

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 格式中的外观。


推荐阅读