scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.26k stars 499 forks source link

Font .name doesn't work #904

Closed yetong2023 closed 8 months ago

yetong2023 commented 11 months ago

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 question, where can I get font name list?

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:
                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')
MartinPacker commented 11 months ago

On Stack Overflow I asked you to clarify what "doesn't work" means.

yetong2023 commented 11 months ago

It seems just change the name of font. But didn't set it to "Calibri" really.

font.name='Calibri'
print(font.name)
out: Calibri

However, when I open PPT file and check, the font didn't change to Calibri. It remains "Times New Roman". So, "font.name=Calibri" didn't work, it didn't change the font type to Calibri.

jesse-sealand commented 10 months ago

@yetong2023, you are close, try setting the font attributes to the current run for each iteration:

            for run in para.runs:
                run.font.size = pptx.util.Pt(20)
                run.font.color.rgb = pptx.dml.color.RGBColor(140,0,0)
                run.font.name = 'Calibri'
stormtozero commented 8 months ago

pptx 字体修改不支持utf-8的字符。