简体   繁体   English

设置wordx的背景颜色

[英]Setting background color of wordx

im trying to set a report default background color but it appears black instead of blue, can anyone help?我正在尝试设置报告默认背景颜色,但它显示为黑色而不是蓝色,有人可以帮忙吗?

Why is that?这是为什么? do i need to use another format that is not hex?我需要使用另一种不是十六进制的格式吗? iv tryied with other format and still nothing. iv 尝试使用其他格式,但仍然一无所获。

        docpath="/Users/ricardosimoes/Desktop/DESKTOP/Jira/my_word_file.docx"
        mydoc = docx.Document()
        section_h = mydoc.sections[0]
        header = section_h.header


        styles = mydoc.styles
        style = styles.add_style('Tahoma',WD_STYLE_TYPE.PARAGRAPH)
        style.font.name = 'Tahoma'
        style.font.size = Pt(11)

        shd = OxmlElement('w:background')
        # Add attributes to the xml element
        shd.set(qn('w:color'), '#0000FF')
        shd.set(qn('w:themeColor'), 'text1')
        shd.set(qn('w:themeTint'), 'F2')
        # Add background element at the start of Document.xml using below
        mydoc.element.insert(0, shd)
        # Add displayBackgroundShape element to setting.xml
        shd1 = OxmlElement('w:displayBackgroundShape')
        mydoc.settings.element.insert(0, shd1)

        paragraph_h = header.paragraphs[0]
        runheader = paragraph_h.add_run()
        runheader.add_picture("client_report/report_img/titulo.png", width=docx.shared.Inches(5), height=docx.shared.Inches(1))


        mydoc.add_picture("client_report/report_img/bottom.png", width=docx.shared.Inches(5),
                          height=docx.shared.Inches(1))
        mydoc.save(docpath)

The snippet seems to supply all three of the w:color , w:themeColor and w:themeTint attributes, and the latter two override the first one.该片段似乎提供了所有三个w:colorw:themeColorw:themeTint属性,后两个覆盖第一个。 The ECMA-376 standard says about the w:color attribute: ECMA-376 标准说明了w:color属性:

If the background specifies the use of a theme color via the themeColor attribute, this value is ignored.如果背景通过 themeColor 属性指定使用主题颜色,则忽略此值。 [Note: Applications are discouraged from specifying both the color and themeColor attributes on the same parent element. [注意:不鼓励应用程序在同一个父元素上同时指定颜色和主题颜色属性。 end note]尾注]

I haven't tested this, but removing themeColor and themeTint (and dropping the # from 0000FF ) should cause the blue color to show up.我没有对此进行测试,但是删除themeColorthemeTint (并从0000FF中删除# )应该会导致蓝色出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM