How can we change the font color of a word in Python?

To change the font color in Word, you can use the python-docx library in Python to manipulate Word documents. Here is an example code demonstrating how to modify the font color in a Word document:

from docx import Document
from docx.shared import RGBColor

# 打开Word文档
doc = Document('example.docx')

# 遍历文档中的段落
for paragraph in doc.paragraphs:
    # 遍历段落中的所有文字
    for run in paragraph.runs:
        # 修改字体颜色为红色
        run.font.color.rgb = RGBColor(255, 0, 0)

# 保存修改后的文档
doc.save('example_modified.docx')

This code opens a Word document named example.docx, then iterates through all paragraphs and text in the document, changes the font color to red, and saves it as example_modified.docx. You can choose other colors by modifying the RGB values as needed.

bannerAds