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

aelbi1ox  于 4个月前  发布在  Python
关注(0)|答案(2)|浏览(105)

下面是我的代码:

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一直在重复“默认大小”。
我尽了最大的努力来获取文本框的字体大小。我已经阅读了文档,但可以找到任何有效的答案为我的问题。我希望,任何人都可以帮助。

hmtdttj4

hmtdttj41#

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

if font.size < Pt(24):

字符串
这对我很有效
它需要:

from pptx.util import Pt

vktxenjb

vktxenjb2#

此问题可能是由于在运行级别访问字体大小造成的,在运行级别可能未显式设置字体大小。请尝试在段落级别访问字体大小。

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:
                font_size = paragraph.style.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: {paragraph.text}, Font size: {font_size_pt}")

字符串

相关问题