Python-pptx字体.name不起作用。如何更改所有字体?

huangapple go评论109阅读模式
英文:

Python-pptx font .name doesn't work. How to change all fonts?

问题

我想将所有字体设置为"Calibri",我使用以下代码成功地更改了字体大小和颜色。然而,font.name却无效。我尝试了构建另一个ppt并再次尝试,但仍然失败。

还有一个问题,我在哪里可以获取字体名称列表?

"tmp.pptx"只是通过MS PowerPoint创建的普通ppt文件。任何人都可以通过打开PowerPoint来复制这个过程。我在其中放置了一个矩形并输入了一些文本,这是在PowerPoint中完成的。现在,我想使用python-pptx包而不是MS PowerPoint将字体类型更改为'Calibri'。问题是我无法将字体类型更改为我设置的font.name='Calibri'
运行代码后,我通过Powerpoint打开tmp.pptx,发现字体仍然是Times New Roman,没有更改为Calibri

总之,我想知道如何将整个PPT中的所有字体从一种类型更改为另一种类型,比如Calibri\Arial等。我的意思是每个幻灯片中的每个字符都更改为我想要的字体。

ppt = pptx.Presentation('tmp.pptx')
for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            for para in shape.text_frame.paragraphs:
                # 如果使用"font=para.font",它无法识别自动形状中的字体。要更改所有字体,应该使用"for run in para.runs"。
                for run in para.runs:
                    font = run.font
                    font.size = pptx.util.Pt(20)  # 成功
                    font.color.rgb = pptx.dml.color.RGBColor(140, 0, 0)  # 成功
                    font.name = 'Calibri'  # 失败
ppt.save('tmp.pptx')
英文:

I want to set all font to "Calibri", I use the following code to change font size,color sucessfully. font.name doesn't work however. I build a another ppt and try again, failed again.

And one more question, where can I get font name list?

"tmp.pptx" is just a common ppt file built through MS powerpoint. Any one can replicate this by open powerpoint. I put a rectangle in it and type some text, which was done in powerpoint. Now I want change the font type to 'Calibri' by using python-pptx pacakage rather than MS powerpoint. The problem is I can not change font type to what I set font.name='Calibri".
After running the code, I open tmp.pptx through Powerpoint and found that the font remains Times New Roman, it didn't change to Calibri.

In a word, How can I change all fonts from one type to another type such as Calibri\Arial whatever in the entire PPT. I mean every character in every slide change to the font I want.

ppt=pptx.Presentation('tmp.pptx')
for slide in ppt.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            for para in shape.text_frame.paragraphs:

#If use "font=para.font",It can not identify font in autoshape. To change all fonts, a "for run in para.runs" should be used.

                for run in para.runs:
                    font=run.font
                    font.size=pptx.util.Pt(20)#succeeded
                    font.color.rgb=pptx.dml.color.RGBColor(140,0,0)#succeeded
                    font.name='Calibri'#failed
ppt.save('tmp.pptx')

答案1

得分: 1

你可以按照以下方式获取字体名称:

from pptx.text.fonts import FontFiles

fonts = FontFiles._installed_fonts()
font_names = [i[0] for i in fonts]

请注意,这是获取字体名称的代码示例。

英文:

You can get font names like this

from pptx.text.fonts import FontFiles

fonts = FontFiles._installed_fonts()
font_names = [i[0] for i in fonts]

huangapple
  • 本文由 发表于 2023年7月27日 16:44:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定