Learn how to create PowerPoint presentations using Python. Modify existing presentations and insert slides, text, images, tables, and charts using Python: https://blog.aspose.com/2021/12/31/create-powerpoint-presentations-in-python/
Last active
December 31, 2021 14:55
-
-
Save aspose-com-gists/51b39b78eacada9c434aebca4afce0d3 to your computer and use it in GitHub Desktop.
Create PowerPoint Presentations in Python | Insert Slides, Tables, Text, Images, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment