python docx 标题字体颜色_使用docx python库,如何同时应用颜色和字体大小

I am writing to an .docx file using python docx library. I want to prespecify the font size and color of a paricular sentence. My problem is that I am not able to do it simultaneously. Let me illustrate -

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')

Result:

I am very well aware that I am setting the color Red to the second sentence, and since there is an = before Pt(22) and RGBColor(255,00) so I cannot apply fontsize and color simultaneously

Is there a way to apply both attributes simultaneously?

Editted: I want the line I want this sentence colored red with fontsize=22 in Red color.

解决方案

maybe you can make this

document=Document()

p=document.add_heading(level=0)

wp = p.add_run('I want this sentence colored red with fontsize=22')

wp.font.size = Pt(22)

wp.font.color.rgb = RGBColor(255,0,0)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值