Write text in particular font color in MS word using python-docx

守給你的承諾、 提交于 2019-11-29 09:53:39

I have found the answer myself using python-docx docs,

Here is the correct code:

from docx import Document
from docx.shared import RGBColor
document = Document()
run = document.add_paragraph().add_run('some text')
font = run.font
font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
p=document.add_paragraph('aaa')
document.save('demo1.docx')

'some text' is a parameter of add_run() function rather than add_paragraph() function.

The above code gives desired color.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!