Automating PowerPoint and Data Visualization Using Python (win32com, python-pptx, seaborn)
This tutorial demonstrates how to automate PowerPoint creation, modification, and data extraction with Python libraries such as win32com and python-pptx, and how to generate and embed data visualizations using seaborn, providing complete code examples for slides, tables, charts, shapes, and images.
1. What Can PPT Automation Do? Advantages
It can automatically generate PPT files, reduce the time spent on formatting, ensure consistent report styles, and overall improve work efficiency.
2. Using win32com to Operate PPT
2.1 Install win32com
<code>pip install pypiwin32</code>The package may already be installed.
2.2 Copy PPT Template with win32com
Python‑pptx does not support template copying well, so win32com is used to copy a slide and then python‑pptx adds content.
<code>import win32com
from win32com.client import Dispatch
import os
ppt = Dispatch('PowerPoint.Application')
ppt.Visible = 1
ppt.DisplayAlerts = 0
pptSel = ppt.Presentations.Open(os.getcwd() + "\\2.2 win32 ppt测试.pptx")
pptSel.Slides(1).Copy()
pageNums = 10
for i in range(pageNums):
pptSel.Slides.Paste()
pptSel.SaveAs(os.getcwd() + "\\win32_copy模板.pptx")
pptSel.Close()
ppt.Quit()</code>3. python-pptx: Create, Copy, Delete Slides
3.1 Install python-pptx
<code>pip install python-pptx</code>3.2 Copy Slides
Because python‑pptx lacks a direct copy method, two approaches are suggested: use win32com to copy the template slide, or duplicate the template and delete unwanted slides with python‑pptx.
3.3 Delete Slides
<code>from pptx import Presentation
def del_slide(prs, index):
slides = list(prs.slides._sldIdLst)
prs.slides._sldIdLst.remove(slides[index])
def fun3_3():
ppt = Presentation('python-pptx 多页待删除模板.pptx')
number_pages = len(ppt.slides)
print("Before delete", number_pages, "pages")
delPageNums = 3
for index in range(delPageNums):
del_slide(ppt, 0)
number_pages = len(ppt.slides)
print("After delete", number_pages, "pages")
ppt.save('python-pptx 多页已删除模板.pptx')
print('Generation complete')
if __name__ == '__main__':
fun3_3()
</code>4. python-pptx: Insert Text, Tables, Charts, Shapes
4.1 Add Text and Set Styles
4.1.1 Single‑line and Multi‑line Text
<code>from pptx import Presentation
from pptx.util import Pt, Cm
ppt = Presentation('4. python-pptx操作模板.pptx')
slide = ppt.slides[0]
# single line
left, top, width, height = Cm(16.9), Cm(1), Cm(12), Cm(1.2)
textbox = slide.shapes.add_textbox(left, top, width, height).text_frame
textbox.paragraphs[0].text = "我是单行内容"
textbox.paragraphs[0].font.size = Pt(15)
# multi line
left, top, width, height = Cm(16.9), Cm(3), Cm(12), Cm(3.6)
textbox2 = slide.shapes.add_textbox(left, top, width, height).text_frame
textbox2.paragraphs[0].text = """我是多行内容1
我是多行内容2
我是多行内容3
"""
textbox2.paragraphs[0].font.size = Pt(15)
ppt.save('4.1 添加文字.pptx')
</code>4.1.2 Text Box Style and Font Style
<code>from pptx import Presentation
from pptx.util import Pt, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_VERTICAL_ANCHOR, PP_ALIGN, PP_ALIGN
ppt = Presentation('4. python-pptx操作模板.pptx')
slide = ppt.slides[0]
left, top, width, height = Cm(16.9), Cm(1), Cm(12), Cm(1.2)
textBox = slide.shapes.add_textbox(left, top, width, height)
fill = textBox.fill
fill.solid()
fill.fore_color.rgb = RGBColor(187, 255, 255)
line = textBox.line
line.color.rgb = RGBColor(0, 255, 0)
line.width = Cm(0.1)
tf = textBox.text_frame
tf.margin_bottom = Cm(0.1)
tf.margin_left = 0
tf.vertical_anchor = MSO_VERTICAL_ANCHOR.BOTTOM
tf.word_wrap = True
tf.paragraphs[0].text = '这是一段文本框里的文字'
tf.paragraphs[0].alignment = PP_ALIGN.CENTER
tf.paragraphs[0].font.name = '微软雅黑'
tf.paragraphs[0].font.bold = True
tf.paragraphs[0].font.italic = True
tf.paragraphs[0].font.color.rgb = RGBColor(255, 0, 0)
tf.paragraphs[0].font.size = Pt(20)
ppt.save('4.1.2 设置文字框与字体样式.pptx')
</code>4.2 Add Table and Set Styles
<code>from pptx import Presentation
from pptx.util import Pt, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import MSO_ANCHOR, PP_ALIGN
ppt = Presentation('4. python-pptx操作模板.pptx')
slide = ppt.slides[0]
left, top, width, height = Cm(6), Cm(12), Cm(13.6), Cm(5)
shape = slide.shapes.add_table(6, 7, left, top, width, height)
table = shape.table
# set column widths, row heights, merge header, fill data, style cells … (code omitted for brevity)
ppt.save('4.2 python-pptx 添加表格并设置样式.pptx')
</code>4.3 Add Chart and Set Styles
<code>from pptx import Presentation
from pptx.util import Pt, Cm
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE
ppt = Presentation('4. python-pptx操作模板.pptx')
slide = ppt.slides[0]
chart_data = ChartData()
content_arr = [
["4/30-5/14", "DVT1", "20", "12", "22", "25", "5"],
["5/15-5/21", "DVT1", "25", "32", "42", "30", "8"],
["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"],
["5/22-6/28", "DVT1", "1", "27", "37", "56", "12"]
]
chart_data.categories = [row[0] for row in content_arr]
chart_data.add_series('问题总数', [row[4] for row in content_arr])
chart_data.add_series('遗留问题总数', [row[5] for row in content_arr])
chart_data.add_series('遗留致命严重\n问题总数', [row[6] for row in content_arr])
left, top, width, height = Cm(6), Cm(10), Cm(16.1), Cm(7.5)
chart = slide.shapes.add_chart(XL_CHART_TYPE.LINE, left, top, width, height, chart_data).chart
chart.has_legend = True
chart.legend.include_in_layout = False
chart.font.size = Pt(10)
ppt.save('4.3 python-pptx 添加图表并设置样式.pptx')
</code>4.4 Add Shapes and Set Styles
<code>from pptx import Presentation
from pptx.util import Pt, Cm
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN
from pptx.enum.shapes import MSO_SHAPE
ppt = Presentation('4. python-pptx操作模板.pptx')
slide = ppt.slides[0]
# rectangle
left, top, width, height = Cm(2.5), Cm(4.5), Cm(30), Cm(0.5)
rect = slide.shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)
rect.fill.solid()
rect.fill.fore_color.rgb = RGBColor(34, 134, 165)
rect.line.color.rgb = RGBColor(34, 134, 165)
# flowchart extract with text (normal and delayed)
# … (additional shape examples omitted for brevity)
ppt.save('4.4 python-pptx 添加形状并设置样式.pptx')
</code>5. seaborn: Introduction and Usage
seaborn is a high‑level Python visualization library built on matplotlib, providing attractive statistical graphics.
5.1 Install seaborn
<code>pip install seaborn</code>5.2 Line Plots
5.2.1 Using relplot
<code>import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset('fmri')
sns.relplot(x='timepoint', y='signal', kind='line', data=data, ci=None)
plt.show()
</code>5.2.2 Using lineplot
<code>import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset('fmri')
sns.lineplot(x='timepoint', y='signal', data=data, ci=95)
plt.show()
</code>5.2.3 Multiple Subplots
<code>import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset('fmri')
f, axes = plt.subplots(1, 2, figsize=(14, 6))
sns.lineplot(x='timepoint', y='signal', data=data, ci=None, ax=axes[0])
sns.lineplot(x='timepoint', y='signal', hue='region', style='event', data=data, ci=None, ax=axes[1])
plt.show()
</code>5.3 Scatter Plot with relplot
<code>import matplotlib.pyplot as plt
import seaborn as sns
tips = sns.load_dataset('tips')
sns.relplot(x='total_bill', y='tip', data=tips, hue='sex', style='smoker', size='size')
plt.show()
</code>5.4 Bar Plot
<code>import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams['axes.unicode_minus'] = False
sns.set_style('darkgrid', {'font.sans-serif':['SimHei','Arial']})
plt.figure(dpi=150)
x = ['金融','农业','制造业','新能源']
y = [164, 86, 126, 53]
sns.barplot(x, y)
plt.show()
</code>6. Insert Images into PowerPoint with python-pptx
<code>from pptx import Presentation
from pptx.util import Pt, Cm
ppt = Presentation('6.python-pptx操作模板.pptx')
slide = ppt.slides[0]
img_name = 'seaborn生成的图片.png'
left, top, width, height = Cm(6), Cm(6), Cm(20), Cm(9)
slide.shapes.add_picture(img_name, left, top, width, height)
ppt.save('6.python-pptx 插入图片.pptx')
</code>7. Read Data from an Existing PowerPoint
<code>from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
ppt = Presentation('研发管理部检测部周报2020-09-17.pptx')
slide0 = ppt.slides[0]
for shape in slide0.shapes:
print(shape.shape_type)
if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
for row in shape.table.rows:
for cell in row.cells:
print(cell.text_frame.text)
</code>The script prints the type of each shape on the first slide and extracts all text from tables, demonstrating how to read PPT content programmatically.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.