python_修改PPT中字体,run.font.name只对英文生效怎么办?
参考:使用pptx_ea_font库设置中文字体
from pptx import Presentation
from pptx.util import Pt
from pptx_ea_font import set_font# 打开现有的 PPT 文件
prs = Presentation('D:\\desktop\\测试论文\\PPT模板_正文.pptx')# 遍历每一张幻灯片
for slide in prs.slides:# 遍历每个形状for shape in slide.shapes:if shape.has_text_frame:text_frame = shape.text_frame# 遍历每个段落for paragraph in text_frame.paragraphs:# 遍历每个运行(run)for run in paragraph.runs:# 输出调试信息print(f"Original text: {run.text}, Original font: {run.font.name}")# 设置英文字体或数字run.font.name = '华文行楷'# 设置中文字体set_font(run, '华文行楷')run.font.size = Pt(12) # 设置字体大小run.font.bold = False # 设置加粗run.font.italic = False # 设置斜体run.font.underline = False # 设置下划线# 输出调试信息print(f"Modified text: {run.text}, Modified font: {run.font.name}")# 保存修改后的 PPT 文件
prs.save('D:\\desktop\\测试论文\\你好.pptx')