Search code examples
pythonpython-docx

How can I change letters’ color?


I want to write letters in docx.I wrote codes,

from docx import Document

document = Document()

document.add_heading("TITLE", 1)

document.save("test.docx")

But when I run the codes, the letter of TITLE is colored blue.I want to add color black to TITLE.I searched the way of it in docx document, but I cannot find it.How should I do it?


Solution

  • Possible duplicate of:

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

    Using docx python library, how to apply color and font size simultaneously

    You would need to use the font property.

    from docx import Document
    
    document = Document()
    
    run = document.add_heading().add_run("TITLE")
    
    font = run.font
    font.color.rgb = RGBColor(0,0,0)
    
    document.save("test.docx")
    

    Please see the python-docx documentation.