Productivity
PowerPoint Processing (PPTX)
Create and manipulate Microsoft PowerPoint presentations programmatically. Use when users need to generate, edit, or convert PowerPoint files.
Create and manipulate Microsoft PowerPoint presentations programmatically. Use when users need to generate, edit, or convert PowerPoint files.
Create and manipulate Microsoft PowerPoint presentations programmatically.
pip install python-pptx
from pptx import Presentation
# Create presentation
prs = Presentation()
# Add slide
slide_layout = prs.slide_layouts[0] # Title slide
slide = prs.slides.add_slide(slide_layout)
# Add content
title = slide.shapes.title
title.text = "Hello, World!"
# Save
prs.save('presentation.pptx')
# Available layouts
layouts = {
0: 'Title Slide',
1: 'Title and Content',
2: 'Section Header',
3: 'Two Content',
4: 'Comparison',
5: 'Title Only',
6: 'Blank',
7: 'Content with Caption',
8: 'Picture with Caption',
}
from pptx import Presentation
prs = Presentation()
# Title slide
slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Presentation Title"
subtitle.text = "Subtitle"
# Content slide
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
title.text = "Section Title"
body = slide.placeholders[1]
body.text = "Content goes here"
from pptx.util import Inches, Pt
# Add text box
txBox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(5), Inches(2))
tf = txBox.text_frame
tf.text = "Hello, World!"
# Format text
p = tf.paragraphs[0]
p.text = "Formatted text"
p.font.size = Pt(24)
p.font.bold = True
p.font.color.rgb = RGBColor(0, 0, 255)
from pptx.util import Inches
# Add image
slide.shapes.add_picture('image.png', Inches(1), Inches(1), Inches(4), Inches(3))
from pptx.enum.shapes import MSO_SHAPE
# Add shape
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(1), Inches(2), Inches(1)
)
shape.fill.solid()
shape.fill.fore_color.rgb = RGBColor(0, 128, 255)
shape.line.color.rgb = RGBColor(0, 0, 0)
from pptx.util import Inches
# Add table
rows, cols = 4, 3
table = slide.shapes.add_table(rows, cols, Inches(1), Inches(1), Inches(6), Inches(3)).table
# Set column widths
table.columns[0].width = Inches(2)
table.columns[1].width = Inches(2)
table.columns[2].width = Inches(2)
# Add data
for i, row in enumerate(table.rows):
for j, cell in enumerate(row.cells):
cell.text = f'Row {i}, Col {j}'
from pptx import Presentation
prs = Presentation()
# Access master
master = prs.slide_masters[0]
# Modify master
for shape in master.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
paragraph.font.name = 'Arial'
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
# Create chart data
chart_data = CategoryChartData()
chart_data.categories = ['January', 'February', 'March']
chart_data.add_series('Series 1', (10, 20, 30))
# Add chart
chart = slide.shapes.add_chart(
XL_CHART_TYPE.COLUMN_CLUSTERED,
Inches(1), Inches(1), Inches(6), Inches(4),
chart_data
).chart
from pptx.enum.shapes import MSO_SHAPE
# Add shape
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(1), Inches(2), Inches(1)
)
# Add animation
from pptx.oxml.ns import qn
from lxml import etree
animation = shape._element.spPr.makeelement(qn('p:anim'), {})
# Configure animation...
from pptx import Presentation
# Load template
prs = Presentation('template.pptx')
# Modify slides
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
for paragraph in shape.text_frame.paragraphs:
if '{{TITLE}}' in paragraph.text:
paragraph.text = paragraph.text.replace('{{TITLE}}', 'New Title')
# Save
prs.save('output.pptx')