首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python-docx设置普通文本的背景色(底纹)

问使用python-docx设置普通文本的背景色(底纹)
EN

Stack Overflow用户
提问于 2020-05-15 00:02:37
回答 2查看 1.9K关注 0票数 4

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

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

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

高亮显示只会改变文本后面的颜色,而阴影则会绘制整条线的颜色--我觉得这两种方式都没问题。

有没有可能使用python-docx在不在表格中的文本后面设置任何RGB值?

​

​

EN

回答 2

Stack Overflow用户

发布于 2020-08-09 00:57:30

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

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

代码语言:javascript
复制
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.oxml.shared import OxmlElement

这些模块非常重要,因为您需要访问run对象的"XML版本“,并在<w:rPr>下添加一个<w:shd>元素,该元素将设置阴影。

代码语言:javascript
复制
# 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')

在print out命令中,它将输出:

代码语言:javascript
复制
<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元素,但我执行了以下操作:

代码语言:javascript
复制
# This is the tricky part
run.font.size = Pt(14)
print(run.element.xml)

在第二个print out命令之后,它将输出:

代码语言:javascript
复制
<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>

代码语言:javascript
复制
tag.rPr.append(shd)
print(run.element.xml)

下面是最终的打印输出命令:

代码语言:javascript
复制
<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>

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

代码语言:javascript
复制
# Save document
doc.save('d.docx')

把所有的东西放在一起:

代码语言:javascript
复制
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的文档,其中提供了一个示例,以展示word文档在XML格式中的样子。

票数 5
EN

Stack Overflow用户

发布于 2021-11-06 00:08:23

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

代码语言:javascript
复制
    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')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61801936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档