如何使用python访问pptx文件中文本框架内文本的字体大小

问题描述 投票:0回答:1

这是我的代码:

from pptx import Presentation

    pptx_file = 'education.pptx'
    presentation = Presentation(pptx_file)

    for slide_number, slide in enumerate(presentation.slides):
        # Iterate through each shape in the slide
        for shape in slide.shapes:
            if shape.has_text_frame:
                # Iterate through each paragraph in the text frame
                for paragraph in shape.text_frame.paragraphs:
                    # Iterate through each run in the paragraph
                    for run in paragraph.runs:
                        font_size = run.font.size
                        # font_size is in Pt, convert to a human-readable format if necessary
                        font_size_pt = font_size.pt if font_size else 'Default size'
                        print(f"Slide {slide_number + 1}, Text: {run.text}, Font size: {font_size_pt}")

我正在尝试使用 python-pptx 访问 pptx 文件中文本框架内文本的字体大小。 Hovewer 不断重复“默认尺寸”。

我尽力获取文本框架的字体大小。我已阅读文档,但可以找到我的问题的任何有效答案。我希望,任何人都可以帮忙。

python powerpoint python-pptx
1个回答
0
投票

在我的 (md2pptx) 项目中,我有这样一行:

if font.size < Pt(24):

这对我有用。

它需要:

from pptx.util import Pt
© www.soinside.com 2019 - 2024. All rights reserved.