ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python/python-pptx 사용자 가이드 - 3. 시작
    코딩/Python 2024. 6. 5. 16:57
    728x90

    https://python-pptx.readthedocs.io/en/latest/user/quickstart.html

    Getting Started

    A quick way to get started is by trying out some of the examples below to get a feel for how to use python-pptx.

    The API documentation can help you with the fine details of calling signatures and behaviors.

    시작하는 빠른 방법은 아래 예제 중 일부를 시도하여 python-pptx 사용 방법에 대한 느낌을 얻는 것이다.

    API 문서는 서명 및 동작 호출에 대한 세부 정보를 제공하는 데 도움이 될 수 있다.

    Hello World! example

    https://python-pptx.readthedocs.io/en/latest/_images/hello-world.png

    from pptx import Presentation
    
    prs = Presentation()
    title_slide_layout = prs.slide_layouts[0]
    slide = prs.slides.add_slide(title_slide_layout)
    title = slide.shapes.title
    subtitle = slide.placeholders[1]
    
    title.text = "Hello, World!"
    subtitle.text = "python-pptx was here!"
    
    prs.save('test.pptx')

    Bullet slide example

    https://python-pptx.readthedocs.io/en/latest/_images/bullet-slide.png

    from pptx import Presentation
    
    prs = Presentation()
    bullet_slide_layout = prs.slide_layouts[1]
    
    slide = prs.slides.add_slide(bullet_slide_layout)
    shapes = slide.shapes
    
    title_shape = shapes.title
    body_shape = shapes.placeholders[1]
    
    title_shape.text = 'Adding a Bullet Slide'
    
    tf = body_shape.text_frame
    tf.text = 'Find the bullet slide layout'
    
    p = tf.add_paragraph()
    p.text = 'Use _TextFrame.text for first bullet'
    p.level = 1
    
    p = tf.add_paragraph()
    p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets'
    p.level = 2
    
    prs.save('test.pptx')

    Not all shapes can contain text, but those that do always have at least one paragraph, even if that paragraph is empty and no text is visible within the shape. _BaseShape.has_text_frame can be used to determine whether a shape can contain text. (All shapes subclass _BaseShape.) When _BaseShape.has_text_frame is True, _BaseShape.text_frame.paragraphs[0] returns the first paragraph. The text of the first paragraph can be set using text_frame.paragraphs[0].text. As a shortcut, the writable properties _BaseShape.text and _TextFrame.text are provided to accomplish the same thing. Note that these last two calls delete all the shape’s paragraphs except the first one before setting the text it contains.

    모든 도형에 텍스트가 포함될 수 있는 것은 아니지만 해당 단락이 비어 있고 도형 내에 텍스트가 표시되지 않는 경우에도 항상 하나 이상의 단락이 포함된다. _BaseShape.has_text_frame을 사용하면 모양에 텍스트가 포함될 수 있는지 여부를 확인할 수 있다. (모든 모양 하위 클래스는 _BaseShape이다.) _BaseShape.has_text_frame이 True인 경우 _BaseShape.text_frame.paragraphs[0]는 첫 번째 단락을 반환한다. 첫 번째 단락의 텍스트는 text_frame.paragraphs[0].text를 사용하여 설정할 수 있다. 바로 가기로 쓰기 가능한 속성인 _BaseShape.text 및 _TextFrame.text가 제공되어 동일한 작업을 수행한다. 마지막 두 번의 호출은 포함된 텍스트를 설정하기 전에 첫 번째 단락을 제외하고 도형의 모든 단락을 삭제한다.

    add_textbox() example

    https://python-pptx.readthedocs.io/en/latest/_images/add-textbox.png

    from pptx import Presentation
    from pptx.util import Inches, Pt
    
    prs = Presentation()
    blank_slide_layout = prs.slide_layouts[6]
    slide = prs.slides.add_slide(blank_slide_layout)
    
    left = top = width = height = Inches(1)
    txBox = slide.shapes.add_textbox(left, top, width, height)
    tf = txBox.text_frame
    
    tf.text = "This is text inside a textbox"
    
    p = tf.add_paragraph()
    p.text = "This is a second paragraph that's bold"
    p.font.bold = True
    
    p = tf.add_paragraph()
    p.text = "This is a third paragraph that's big"
    p.font.size = Pt(40)
    
    prs.save('test.pptx')

    add_picture() example

    https://python-pptx.readthedocs.io/en/latest/_images/add-picture.png

    from pptx import Presentation
    from pptx.util import Inches
    
    img_path = 'monty-truth.png'
    
    prs = Presentation()
    blank_slide_layout = prs.slide_layouts[6]
    slide = prs.slides.add_slide(blank_slide_layout)
    
    left = top = Inches(1)
    pic = slide.shapes.add_picture(img_path, left, top)
    
    left = Inches(5)
    height = Inches(5.5)
    pic = slide.shapes.add_picture(img_path, left, top, height=height)
    
    prs.save('test.pptx')

    add_shape() example

    https://python-pptx.readthedocs.io/en/latest/_images/add-shape.png

    from pptx import Presentation
    from pptx.enum.shapes import MSO_SHAPE
    from pptx.util import Inches
    
    prs = Presentation()
    title_only_slide_layout = prs.slide_layouts[5]
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes
    
    shapes.title.text = 'Adding an AutoShape'
    
    left = Inches(0.93)  # 0.93" centers this overall set of shapes
    top = Inches(3.0)
    width = Inches(1.75)
    height = Inches(1.0)
    
    shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height)
    shape.text = 'Step 1'
    
    left = left + width - Inches(0.4)
    width = Inches(2.0)  # chevrons need more width for visual balance
    
    for n in range(2, 6):
        shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)
        shape.text = 'Step %d' % n
        left = left + width - Inches(0.4)
    
    prs.save('test.pptx')

    Constants representing each of the available auto shapes (like MSO_SHAPE.ROUNDED_RECT, MSO_SHAPE.CHEVRON, etc.) are listed on the autoshape-types page.

    사용 가능한 각 자동 모양(예: MSO_SHAPE.ROUNDED_RECT, MSO_SHAPE.CHEVRON 등)을 나타내는 상수는 자동 모양 유형 페이지에 나열된다.

    add_table() example

    https://python-pptx.readthedocs.io/en/latest/_images/add-table.png

    from pptx import Presentation
    from pptx.util import Inches
    
    prs = Presentation()
    title_only_slide_layout = prs.slide_layouts[5]
    slide = prs.slides.add_slide(title_only_slide_layout)
    shapes = slide.shapes
    
    shapes.title.text = 'Adding a Table'
    
    rows = cols = 2
    left = top = Inches(2.0)
    width = Inches(6.0)
    height = Inches(0.8)
    
    table = shapes.add_table(rows, cols, left, top, width, height).table
    
    # set column widths
    table.columns[0].width = Inches(2.0)
    table.columns[1].width = Inches(4.0)
    
    # write column headings
    table.cell(0, 0).text = 'Foo'
    table.cell(0, 1).text = 'Bar'
    
    # write body cells
    table.cell(1, 0).text = 'Baz'
    table.cell(1, 1).text = 'Qux'
    
    prs.save('test.pptx')

    Extract all text from slides in presentation

    from pptx import Presentation
    
    prs = Presentation(path_to_presentation)
    
    # text_runs will be populated with a list of strings,
    # one for each text run in presentation
    text_runs = []
    
    for slide in prs.slides:
        for shape in slide.shapes:
            if not shape.has_text_frame:
                continue
            for paragraph in shape.text_frame.paragraphs:
                for run in paragraph.runs:
                    text_runs.append(run.text)

     

    Python/python-pptx 사용자 가이드

    728x90

    댓글

Designed by Tistory.