[英]How to highlight heading in python-docx
I am able to highlight a run like this:我能够突出显示这样的运行:
p = self.document.add_paragraph(style="List Bullet")
run = p.add_run(articles_date[i] + ": ")
run.font.highlight_color = WD_COLOR_INDEX.YELLOW
But how do I highlight a heading.但是我如何突出一个标题。 This gives error:这给出了错误:
h = self.document.add_heading(country + " " + sourcetype, level=3)
h.font.highlight_color = WD_COLOR_INDEX.YELLOW
Traceback (most recent call last):
File "C:\Users\fatima.arshad\PycharmProjects\pythonProject\main.py", line 111, in <module>
main("." ,"data", "templates", "combined.docx",document = "1", new_data=json_data)
File "C:\Users\fatima.arshad\PycharmProjects\pythonProject\main.py", line 47, in main
document_one.exec()
File "C:\Users\fatima.arshad\PycharmProjects\pythonProject\DocumentOne.py", line 119, in exec
self.create_section_two()
File "C:\Users\fatima.arshad\PycharmProjects\pythonProject\DocumentOne.py", line 80, in create_section_two
h = self.document.add_heading.add_run(country + " " + sourcetype, level=3)
AttributeError: 'function' object has no attribute 'add_run'
Edit:编辑:
I applied color like this我应用了这样的颜色
h = self.document.add_heading(country + " " + sourcetype, level=3)
h.style.font.highlight_color = WD_COLOR_INDEX.YELLOW
But it's not working :/但它不起作用:/
Tried this test code:试过这个测试代码:
doc = docx.Document()
doc.add_paragraph("joiiii")
heading = doc.add_heading("hellow")
heading.style.font.highlight_color = WD_COLOR_INDEX.YELLOW
heading2 = doc.add_heading("new heading")
doc.add_paragraph("joiiii")
doc.save("test.docx")
It highlights even the one not needed to be highlighted它甚至会突出显示不需要突出显示的内容
Highlight is applied at the run level (as is all character formatting) and a heading is a paragraph.突出显示在运行级别应用(与所有字符格式一样),标题是一个段落。 So you need something like this:所以你需要这样的东西:
heading = document.add_heading("my heading")
for run in heading.runs:
run.font.highlight_color = WD_COLOR_INDEX.YELLOW
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.