Python에서 PowerPoint ppt pptx 만들기 및 수정

MS PowerPoint는 차트, 그래픽, 애니메이션 및 기타 요소를 사용하여 대화형 프레젠테이션을 만들 수 있는 기능이 풍부한 응용 프로그램입니다. 프로그래머는 Python 응용 프로그램 내에서 PowerPoint의 프레젠테이션 조작 기능을 자동화해야 할 수도 있습니다. 이 게시물에서는 Python에서 PowerPoint PPT 또는 PPTX 프레젠테이션을 만드는 방법을 배웁니다. 또한 프로그래밍 방식으로 프레젠테이션에 슬라이드, 텍스트, 표, 이미지 및 차트를 추가하는 방법을 다룹니다.

PowerPoint PPT를 만드는 Python 라이브러리

Aspose.Slides for Python via .NET은 MS Office 없이 PowerPoint PPT/PPTX를 만들고 조작할 수 있는 강력한 Python 라이브러리입니다. 또한 기존 프레젠테이션을 업데이트하고 다른 형식으로 변환할 수 있습니다. 다음 pip 명령을 사용하여 PyPI에서 라이브러리를 설치할 수 있습니다.

> pip install aspose.slides

Python에서 PowerPoint PPT 만들기

먼저 기본적으로 추가되는 빈 슬라이드가 있는 PowerPoint 프레젠테이션을 만들어 보겠습니다. 다음 단계는 Python에서 PowerPoint PPT를 만드는 방법을 보여줍니다.

  • 먼저 Presentation 클래스의 인스턴스를 만듭니다.
  • 그런 다음 Presentation.save(string, export.SaveFormat) 메서드를 사용하여 PPT를 저장합니다.

다음 코드 샘플은 Python에서 PowerPoint PPTX 파일을 만드는 방법을 보여줍니다.

import aspose.slides as slides

# Create a new presentation
with slides.Presentation() as presentation:
    
    # Access the default slide
    slide = presentation.slides[0]
    
    # Save the presentation
    presentation.save("create-presentation.pptx", slides.export.SaveFormat.PPTX)

Python에서 PowerPoint PPTX 파일 열기

기존 프레젠테이션을 열고 콘텐츠를 업데이트할 수도 있습니다. 이를 달성하려면 프레젠테이션 생성자에서 프레젠테이션 파일의 경로만 제공하면 됩니다. 다음 코드 샘플은 Python에서 기존 PPT/PPTX 파일을 여는 방법을 보여줍니다.

import aspose.slides as slides

# Open presentation
with slides.Presentation("presentation.ppt") as presentation:
    
    # Access the default slide
    slide = presentation.slides[0]
    
    # Save the presentation
    presentation.save("create-presentation.ppt", slides.export.SaveFormat.PPT)

Python에서 PowerPoint PPT에 슬라이드 추가

이제 이전에 만든 프레젠테이션에 새 슬라이드를 추가해 보겠습니다. 또한 프레젠테이션을 먼저 열어 기존 프레젠테이션에 슬라이드를 추가할 수도 있습니다(이전 섹션 참조). 다음은 Python에서 PPT/PPTX 프레젠테이션에 슬라이드를 추가하는 단계입니다.

  • 먼저 Presentation 클래스를 사용하여 새 프레젠테이션을 만들거나 기존 프레젠테이션을 로드합니다.
  • 그런 다음 Presentation.slides 속성을 사용하여 슬라이드 컬렉션의 참조를 가져옵니다.
  • Presentation.slides.add\empty\slide(LayoutSlide) 메서드를 사용하여 슬라이드를 추가합니다.
  • 마지막으로 Presentation.save(string, export.SaveFormat) 메서드를 사용하여 프레젠테이션을 저장합니다.

다음 코드 샘플은 Python에서 PowerPoint 프레젠테이션에 슬라이드를 추가하는 방법을 보여줍니다.

import aspose.slides as slides

# Create a new presentation
with slides.Presentation() as pres:
    # Get reference of slides
    slds = pres.slides

    # Loop through layout slides
    for i in range(len(pres.layout_slides)):
        # Add an empty slide to the slides collection
        slds.add_empty_slide(pres.layout_slides[i])
        
    # Do some work on the newly added slide

    # Save the PPTX file to the Disk
    pres.save("presentation.pptx", slides.export.SaveFormat.PPTX)

Python에서 PowerPoint PPT에 텍스트 추가

슬라이드를 추가했으면 콘텐츠 추가를 진행할 수 있습니다. 먼저 Python에서 프레젠테이션 슬라이드에 텍스트를 추가하는 방법을 살펴보겠습니다.

  • Presentation 클래스를 사용하여 새 PPT/PPTX를 생성(또는 기존 로드)합니다.
  • Presentation.slides 컬렉션에서 슬라이드 참조를 가져옵니다.
  • Slide.shapes.add\auto\shape(slides.ShapeType.RECTANGLE, int, int, int, int) 메서드를 사용하여 슬라이드에 새 자동 모양을 추가하고 개체에서 모양의 참조를 가져옵니다.
  • add\text\frame(string) 메서드를 사용하여 모양에 텍스트 프레임을 추가합니다.
  • 텍스트 프레임의 기본 단락에 액세스합니다.
  • 단락의 기본 부분에 액세스합니다.
  • 부분에 텍스트를 추가하고 Presentation.save(string, export.SaveFormat) 메서드를 사용하여 프레젠테이션을 저장합니다.

다음 코드 샘플은 Python을 사용하여 PPT의 슬라이드에 텍스트를 추가하는 방법을 보여줍니다.

import aspose.slides as slides

# Create presentation
with slides.Presentation() as pres:
    # Access first slide
    slide = pres.slides[0]

    # Add an AutoShape with type set as Rectangle
    ashp = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 150, 75, 150, 50)

    # Add TextFrame to the Rectangle
    ashp.add_text_frame(" ")

    # Access the text frame
    txtFrame = ashp.text_frame

    # Create the Paragraph object for text frame
    para = txtFrame.paragraphs[0]

    # Create a Portion object for paragraph
    portion = para.portions[0]

    # Set text
    portion.text = "Aspose TextBox"

    # Save the presentation
    pres.save("add-text-in-presentation.pptx", slides.export.SaveFormat.PPTX)

다음은 텍스트를 추가한 후의 슬라이드 스크린샷입니다.

파이썬에서 파워포인트 ppt에 텍스트 추가

자세히 읽기 PowerPoint 프레젠테이션에서 텍스트 작업에 대해.

Python에서 PowerPoint PPT에 표 만들기

표는 문서의 필수적인 부분이며 PowerPoint 프레젠테이션에서도 널리 사용됩니다. Aspose.Slides for Python을 사용하면 프레젠테이션에서 표를 훨씬 쉽게 만들 수 있습니다. 그럼 파이썬을 사용하여 PPT에서 표를 만드는 방법을 살펴보겠습니다.

  • Presentation 클래스를 사용하여 새 PPT/PPTX를 생성(또는 기존 로드)합니다.
  • Presentation.slides 컬렉션에서 슬라이드 참조를 가져옵니다.
  • 너비가 있는 열과 높이가 있는 행의 배열을 정의합니다.
  • Slide.shapes.addTable()을 사용하여 테이블을 만들고 개체에서 테이블의 참조를 가져옵니다.
  • 서식을 적용하려면 셀을 반복합니다.
  • Table.rows[][].text\frame.text 속성을 사용하여 셀에 텍스트를 추가합니다.
  • Presentation.save(string, export.SaveFormat) 메서드를 사용하여 프레젠테이션을 저장합니다.

다음 코드 샘플은 Python을 사용하여 PowerPoint 프레젠테이션에서 표를 만드는 방법을 보여줍니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

# Create presentation
with slides.Presentation() as pres:
    # Access first slide
    slide = pres.slides[0]

    # Define columns with widths and rows with heights
    dblCols =  [50, 50, 50] 
    dblRows =  [50, 30, 30, 30, 30] 

    # Add table shape to slide
    tbl = slide.shapes.add_table(100, 50, dblCols, dblRows)

    # Set border format for each cell
    for row in range(len(tbl.rows)):
        for cell in range(len(tbl.rows[row])):
            tbl.rows[row][cell].cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID
            tbl.rows[row][cell].cell_format.border_top.fill_format.solid_fill_color.color = drawing.Color.blue
            tbl.rows[row][cell].cell_format.border_top.width = 5

            tbl.rows[row][cell].cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID
            tbl.rows[row][cell].cell_format.border_bottom.fill_format.solid_fill_color.color= drawing.Color.blue
            tbl.rows[row][cell].cell_format.border_bottom.width =5

            tbl.rows[row][cell].cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID
            tbl.rows[row][cell].cell_format.border_left.fill_format.solid_fill_color.color =drawing.Color.blue
            tbl.rows[row][cell].cell_format.border_left.width = 5

            tbl.rows[row][cell].cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID
            tbl.rows[row][cell].cell_format.border_right.fill_format.solid_fill_color.color = drawing.Color.blue
            tbl.rows[row][cell].cell_format.border_right.width = 5


    # Merge cells 1 & 2 of row 1
    tbl.merge_cells(tbl.rows[0][0], tbl.rows[1][1], False)

    # Add text to the merged cell
    tbl.rows[0][0].text_frame.text = "Merged Cells"

    # Save the presentation
    pres.save("add-table-in-presentation.pptx", slides.export.SaveFormat.PPTX)

다음은 위의 코드 스니펫의 출력입니다.

Python에서 PowerPoint ppt에 테이블 추가

테이블 생성 및 조작의 다른 기능을 살펴보십시오.

Python에서 PowerPoint PPT로 차트 만들기

다음은 Python에서 PowerPoint 프레젠테이션에 차트를 추가하는 단계입니다.

  • Presentation 클래스를 사용하여 새 프레젠테이션을 만들거나 기존 프레젠테이션을 로드합니다.
  • Presentation.slides 컬렉션에서 슬라이드 참조를 가져옵니다.
  • Slide.shapes.addChart(ChartType, single, single, single, single) 메서드를 사용하여 원하는 유형의 차트를 추가합니다.
  • 차트 데이터 워크시트에 액세스합니다.
  • 모든 기본 시리즈 및 범주를 지웁니다.
  • 새로운 시리즈와 카테고리를 추가하세요.
  • 차트 시리즈에 대한 새 차트 데이터를 추가합니다.
  • 차트 시리즈의 채우기 색상을 설정합니다.
  • 차트 시리즈 레이블을 추가합니다.
  • 프레젠테이션을 PPTX 파일로 저장합니다.

다음 코드 샘플은 Python을 사용하여 PPT에 차트를 추가하는 방법을 보여줍니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

# Create presentation
with slides.Presentation() as pres:
    # Access first slide
    slide = pres.slides[0]

    # Access first slide
    sld = pres.slides[0]

    # Add chart with default data
    chart = sld.shapes.add_chart(slides.charts.ChartType.CLUSTERED_COLUMN, 0, 0, 500, 500)

    # Set chart Title
    chart.chart_title.add_text_frame_for_overriding("Sample Title")
    chart.chart_title.text_frame_for_overriding.text_frame_format.center_text = 1
    chart.chart_title.height = 20
    chart.has_title = True

    # Set first series to Show Values
    chart.chart_data.series[0].labels.default_data_label_format.show_value = True

    # Set the index of chart data sheet
    defaultWorksheetIndex = 0

    # Get the chart data worksheet
    fact = chart.chart_data.chart_data_workbook

    # Delete default generated series and categories
    chart.chart_data.series.clear()
    chart.chart_data.categories.clear()
    s = len(chart.chart_data.series)
    s = len(chart.chart_data.categories)

    # Add new series
    chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 0, 1, "Series 1"), chart.type)
    chart.chart_data.series.add(fact.get_cell(defaultWorksheetIndex, 0, 2, "Series 2"), chart.type)

    # Add new categories
    chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 1, 0, "Caetegoty 1"))
    chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 2, 0, "Caetegoty 2"))
    chart.chart_data.categories.add(fact.get_cell(defaultWorksheetIndex, 3, 0, "Caetegoty 3"))

    # Take first chart series
    series = chart.chart_data.series[0]

    # Now populating series data

    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 1, 1, 20))
    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 2, 1, 50))
    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 3, 1, 30))

    # Set fill color for series
    series.format.fill.fill_type = slides.FillType.SOLID
    series.format.fill.solid_fill_color.color = drawing.Color.red

    # Take second chart series
    series = chart.chart_data.series[1]

    # Now populating series data
    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 1, 2, 30))
    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 2, 2, 10))
    series.data_points.add_data_point_for_bar_series(fact.get_cell(defaultWorksheetIndex, 3, 2, 60))

    # Setting fill color for series
    series.format.fill.fill_type = slides.FillType.SOLID
    series.format.fill.solid_fill_color.color = drawing.Color.orange

    # First label will be show Category name
    lbl = series.data_points[0].label
    lbl.data_label_format.show_category_name = True

    lbl = series.data_points[1].label
    lbl.data_label_format.show_series_name = True

    # Show value for third label
    lbl = series.data_points[2].label
    lbl.data_label_format.show_value = True
    lbl.data_label_format.show_series_name = True
    lbl.data_label_format.separator = "/"

    # Save the presentation
    pres.save("create-chart-in-presentation.pptx", slides.export.SaveFormat.PPTX)

다음 스크린샷은 위의 코드 조각을 사용하여 만든 차트를 보여줍니다.

Python에서 PowerPoint pptx에 차트 추가

PowerPoint 프레젠테이션에서 차트 만들기에 대해 자세히 알아보기

Python에서 PowerPoint PPTX에 이미지 추가

다음은 Python에서 PowerPoint PPTX에 이미지를 추가하는 단계입니다.

  • Presentation 클래스를 사용하여 새 프레젠테이션을 만들거나 기존 프레젠테이션을 로드합니다.
  • Presentation.slides 컬렉션에서 슬라이드 참조를 가져옵니다.
  • open(string, string) 메서드를 사용하여 파일에서 이미지를 엽니다.
  • Presentation.images.add\image() 메서드를 사용하여 프레젠테이션의 이미지 컬렉션에 이미지를 추가합니다.
  • Slide.shapes.add\picture\frame() 메서드를 사용하여 슬라이드에 이미지를 추가합니다.
  • Presentation.save(string, export.SaveFormat) 메서드를 사용하여 프레젠테이션을 저장합니다.

다음 코드 샘플은 Python에서 PowerPoint 프레젠테이션에 이미지를 추가하는 방법을 보여줍니다.

import aspose.slides as slides

# Create presentation
with slides.Presentation() as pres:
    # Access first slide
    slide = pres.slides[0]

    # Load image from file
    with open("python-logo.png", "rb") as in_file:
      
        # Add image to presentation's image collection
        image = pres.images.add_image(in_file)

        # Add image to slide
        slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 10, 10, 100, 100, image)

    # Save the presentation
    pres.save("add-image-in-presentation.pptx", slides.export.SaveFormat.PPTX)

다음은 위 코드 샘플의 출력입니다.

Python에서 PowerPoint PPT에 이미지 추가

PowerPoint 프레젠테이션의 이미지 작업에 대해 자세히 알아보십시오.

무료 라이선스 받기

임시 라이선스를 얻으면 평가 제한 없이 .NET을 통해 Python용 Aspose.Slides를 사용할 수 있습니다.

결론

이 기사에서는 Python에서 처음부터 PowerPoint PPT/PPTX를 만드는 방법을 배웠습니다. PowerPoint 프레젠테이션에서 슬라이드, 텍스트, 표, 차트 및 이미지를 추가하는 방법을 시연했습니다. 또한 문서를 사용하여 Python용 Aspose.Slides의 다른 기능을 탐색할 수 있습니다. 또한 포럼을 통해 문의 사항을 자유롭게 공유해 주십시오.

또한보십시오