设置word文件中的中文字体
来源:3-7 设置Word全局样式和文本样式
Mr朱_
2023-06-06 09:46:56
视频代码中
style.font.name = '微软雅黑'
并不能将中文字体改成微软雅黑,有什么办法设置中文字体?
# coding:utf-8
from docx import Document
from docx.shared import Inches, RGBColor, Pt
doc = Document() # 新建文件不带参数
style = doc.styles['Normal']
# for style in doc.styles: # 查看所有样式
# print(style)
style.font.name = '微软雅黑'
style.font.color.rgb = RGBColor(255, 255, 0)
style.font.size = Pt(16)
title = doc.add_heading('My title', 0)
title.add_run('\n123')
para = doc.add_paragraph('欢迎来到这里学习python')
para.add_run('\n这是关于word生成的知识')
picture = doc.add_picture('logo2020.png', width=Inches(3))
titles = ['name', 'age', 'sex']
table = doc.add_table(rows=1, cols=3)
title_cell = table.rows[0].cells
for i in range(len(title_cell)):
title_cell[i].text = titles[i]
data = [
['xiaomu', 10, 'man'],
['dewei', 34, 'man'],
['xiaoman', 18, 'women']
]
for d in data:
row_cells = table.add_row().cells
for i, j in enumerate(d):
row_cells[i].text = str(j)
doc.add_page_break()
title2 = doc.add_heading('My title2', 0)
doc.add_page_break()
title3 = doc.add_heading('My title3', 0)
doc.save('_练习test.docx')1回答
同学,你好!可以使用style.element.rPr.rFonts.set(qn('w:eastAsia'), u'字体名')使设置的中文字体生效;
from docx.oxml.ns import qn

祝学习愉快~
相似问题