python docx 字体颜色
时间: 2024-01-28 18:14:02 浏览: 187
a_docx模块_
以下是两种设置Python docx字体颜色的例子:
1. 设置整段文字的字体颜色:
```python
from docx import Document
from docx.shared import RGBColor
document = Document()
paragraph = document.add_paragraph("欢迎来到亚丁号")
run = paragraph.runs[0]
run.font.color.rgb = RGBColor(2, 2, 250) # 设置字体颜色为蓝色
document.save("D:\\亚丁号.docx")
```
2. 设置特定关键字后面的文字的字体颜色:
```python
from docx import Document
from docx.shared import RGBColor
document = Document("your_document.docx") # 替换为你的文档路径
for paragraph in document.paragraphs:
if "答案" in paragraph.text:
keyword_index = paragraph.text.index("答案")
run = paragraph.runs[keyword_index]
for i in range(keyword_index + 1, len(paragraph.runs)):
run = paragraph.runs[i]
run.font.color.rgb = RGBColor(255, 255, 255) # 设置字体颜色为白色
document.save("your_modified_document.docx") # 替换为你想保存的文档路径
```
阅读全文