设置word的背景色

Setting background color of wordx

我正在尝试设置报告的默认背景颜色,但它显示为黑色而不是蓝色,有人可以帮忙吗?

这是为什么?我需要使用另一种非十六进制的格式吗? 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)

该片段似乎提供了 w:colorw:themeColorw:themeTint 这三个属性,后两个覆盖了第一个。 ECMA-376 标准对 w:color 属性说:

If the background specifies the use of a theme color via the themeColor attribute, this value is ignored. [Note: Applications are discouraged from specifying both the color and themeColor attributes on the same parent element. end note]

我还没有对此进行测试,但是删除 themeColorthemeTint(并从 0000FF 中删除 #)应该会导致显示蓝色。