You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何修改python-pptx生成演示文稿的默认文本字号(默认32)

Fixing Default 32pt Text Size in python-pptx

Got it, let's break this down for you. First, the reason your slides are defaulting to 32pt text is because python-pptx uses a built-in default theme template, and the default font size for body text in that template is set to 32 points. That's just the out-of-the-box styling the library uses when you don't specify custom styles.

Now, let's get to fixing it—you've got a couple of options depending on whether you want to change the default for all slides or just specific text boxes:

Option 1: Change the Master Slide Default (Global Fix)

If you want every text box added to your slides to use a different default size, modify the slide master's text styles. This way, any new text you add will inherit the new size automatically:

from pptx import Presentation
from pptx.util import Pt

prs = Presentation()

# Access the slide master and its default body text style
slide_master = prs.slide_master
body_style = slide_master.text_styles[1]  # Index 1 corresponds to body text styling
body_style.font.size = Pt(18)  # Set your desired default font size here

# Add a slide and text—this will use the new default size
slide = prs.slides.add_slide(prs.slide_layouts[1])
text_box = slide.shapes.add_textbox(left=100, top=100, width=400, height=300)
tf = text_box.text_frame
tf.text = "This text uses the new global default size (18pt)"

prs.save("global_default_font.pptx")

Option 2: Set Font Size Directly for Specific Text Boxes

If you only need to adjust size for certain text elements (like your TEXT list), set the font size explicitly when adding text:

from pptx import Presentation
from pptx.util import Pt

TEXT = [
    '''Help on method shuffle in module random: shuffle(x, random=None) method of random.Random instance Shuffle list x in place, and return None. Optional argument random is a 0-argument function returning a random float in [0.0, 1.0); if it is the default None, the standard random.random will be used.''',
    '''Help on method choice in module random: choice(seq...'''
]

prs = Presentation()

# Add slides and apply custom font size to each text block
for text_content in TEXT:
    slide = prs.slides.add_slide(prs.slide_layouts[1])
    text_box = slide.shapes.add_textbox(left=100, top=100, width=500, height=400)
    tf = text_box.text_frame
    tf.text = text_content
    
    # Set font size for all runs in the text box
    for paragraph in tf.paragraphs:
        for run in paragraph.runs:
            run.font.size = Pt(16)  # Adjust to your preferred size

prs.save("custom_text_size.pptx")

Option 3: Adjust Placeholder Text Size

If you're using built-in slide layouts with pre-made placeholders (like title or body sections), target those directly to adjust their text size:

from pptx import Presentation
from pptx.util import Pt

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[1])  # Layout with title + body placeholders

# Update body placeholder text and set its font size
body_placeholder = slide.placeholders[1]
tf = body_placeholder.text_frame
tf.text = TEXT[0]

for paragraph in tf.paragraphs:
    paragraph.font.size = Pt(18)

prs.save("placeholder_font_size.pptx")

Pick the option that fits your workflow best—global master changes are great for consistent styling across all slides, while direct adjustments work if you need more control over individual elements.

内容的提问来源于stack exchange,提问作者Danya

火山引擎 最新活动