gpt4 book ai didi

xml - 修改python-docx中paragraph.style._element.xml中的XML

转载 作者:行者123 更新时间:2023-12-04 14:17:41 25 4
gpt4 key购买 nike

我想修改边框的颜色,调用style._element.xml获取其XML:

>>> document = Document()
>>> run = document.add_heading(u'', 0).add_run('hello world')
>>> paragraphs = document.paragraphs
>>> print(paragraphs[0].style._element.xml)
<w:style xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" w:type="paragraph" w:styleId="Title">
<w:name w:val="Title"/>
<w:basedOn w:val="Normal"/>
<w:next w:val="Normal"/>
<w:link w:val="TitleChar"/>
<w:uiPriority w:val="10"/>
<w:qFormat/>
<w:rsid w:val="00FC693F"/>
<w:pPr>
<w:pBdr>
<w:bottom w:val="single" w:sz="8" w:space="4" w:color="4F81BD" w:themeColor="accent1"/>
</w:pBdr>
<w:spacing w:after="300" w:line="240" w:lineRule="auto"/>
<w:contextualSpacing/>
</w:pPr>
<w:rPr>
<w:rFonts w:asciiTheme="majorHAnsi" w:eastAsiaTheme="majorEastAsia" w:hAnsiTheme="majorHAnsi" w:cstheme="majorBidi"/>
<w:color w:val="17365D" w:themeColor="text2" w:themeShade="BF"/>
<w:spacing w:val="5"/>
<w:kern w:val="28"/>
<w:sz w:val="52"/>
<w:szCs w:val="52"/>
</w:rPr>
</w:style>


现在我想更改属性边框中的颜色代码,我该怎么做?

最佳答案

paragraph.style._element 是一个 lxml.etree._Element 对象,所以像这样的东西应该可以解决问题:

from docx.oxml.ns import qn

bottom = paragraph.style._element.xpath("./w:pPr/w:pBdr/w:bottom")[0]
bottom.set(qn("w:color"), "FF00FF")

关于xml - 修改python-docx中paragraph.style._element.xml中的XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58654472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com