我正在尝试创建财务日志,但是我无法将字体大小从一种更改为另一种,而不会更改所有文本的整个字体大小。
我希望“ Finance Log”和“ Created On ...”位于48,而“ Log Begins:”位于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,但是我无法从中解决问题
答案 0 :(得分:0)
我无法将字体大小从一种更改为另一种,而不会更改所有文本的整个字体大小。
那是因为您正在更改style
对象的基础字体大小:
Tstyle = doc.styles['Normal']
font = Tstyle.font # << this line assigns font = doc.styles['Normal'].font
因此,您不使用某些通用的“字体”属性,而是使用属于命名样式“普通”的字体。如此:
font.name = "Nunito Sans"
font.size = Pt(48) # << this line changes the font size of doc.styles['Normal']
未经测试,请尝试以下操作:
TStyle, FStyle = doc.styles['Normal'], doc.styles['Heading 1']
for style in (TStyle, FStyle):
style.font.name = "Nunito Sans"
TStyle.font.size = Pt(48)
FStyle.font.size = Pt(24)
Title = doc.add_paragraph()
Title.style = TStyle
TRun = Title.add_run("Finance Log")
TRun.bold = True
FCreated = Title.add_run("\nFile Created On {0}".format(datetime.datetime.now().strftime('%d-%m-%y')))
FLog = doc.add_paragraph()
FLog.style = FStyle
FinanceTitle = FLog.add_run("Log Begins:")
doc.save(path_to_docx)