如何更改docx文件python中的字体大小

2024-05-14 08:45:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个财务日志,但是我不能改变字体大小从一个大小到另一个没有改变所有文本的整个字体大小。在

我希望“财务日志”和“创建日期…”为48,“日志开始时间:”为24。在

Tstyle = doc.styles['Normal']
font = Tstyle.font
font.name = "Nunito Sans"
font.size = Pt(48)
Title = doc.add_paragraph()
TRun = Title.add_run("Finance Log")
TRun.bold = True
CurrentDate= datetime.datetime.now()
FormattedDate= CurrentDate.strftime('%d-%m-%Y')
FCreated = Title.add_run("\nFile Created On "+FormattedDate)
Fstyle = doc.styles['Heading 1']
font = Fstyle.font
font.name = "Nunito Sans"
font.size = Pt(24)
FLog = doc.add_paragraph()
FinanceTitle = FLog.add_run("Log Begins:")
doc.save(path_to_docx)

我已经尝试了多种方法,比如创建一个新样式,将新样式设置为标题等。。。在

我知道Set paragraph font in python-docx但是我不能从中解决问题


Tags: runnameaddptsizedoctitle财务
1条回答
网友
1楼 · 发布于 2024-05-14 08:45:27

I canot change the font size from one size to another without the entire font size of all the text changing.

这是因为您正在更改style对象的基本字体大小:

Tstyle = doc.styles['Normal']
font = Tstyle.font  # << this line assigns font = doc.styles['Normal'].font

所以,你不是在使用一些通用的“字体”属性,而是使用属于命名样式“Normal”的字体。因此:

^{pr2}$

未经测试,但请尝试以下方法:

^{3}$

相关问题 更多 >

    热门问题