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

前端 未结 1 732
庸人自扰
庸人自扰 2020-12-19 00:08

I am trying to write text in an MS Word file using python library python-docx. I have gone through the documentation of python-docx\'s font color on this link and applied th

1条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-19 00:35

    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.

    0 讨论(0)
提交回复
热议问题