【发布时间】:2018-07-19 06:09:02
【问题描述】:
我正在使用 python docx 库写入 .docx 文件。我想预先指定特定句子的字体大小和颜色。我的问题是我无法同时做到这一点。让我举例说明-
from docx import Document
from docx.shared import Pt #Helps to specify font size
from docx.shared import RGBColor #Helps to specify font Color
document=Document() #Instantiation
p=document.add_heading(level=0)
p.add_run('I want this sentence colored red with fontsize=22').font.size=Pt(22) #Specifies fontsize 22
p.add_run('This line gets colored red').font.color.rgb=RGBColor(255,0,0) #Specifies RED color
document.save('path/file.docx')
我非常清楚我将颜色 Red 设置为第二句,并且由于在 Pt(22) 和 RGBColor(255,00) 之前有一个 = 所以我不能同时应用 fontsize 和 color
有没有办法同时应用这两个属性?
已编辑:我想要红色的线 I want this sentence colored red with fontsize=22。
【问题讨论】:
-
同时做这两个有什么好处?
-
嗯,我的意图是让“我希望这句话用 fontsize=22 染成红色”这一行染成红色。最终结果很重要,手段不重要,所以如果你有其他方法同样好。
标签: python fonts colors font-size python-docx