The Python-pptx library is a Python library for creating, updating, and reading Microsoft PowerPoint .pptx files. It allows us to use Python scripts to automate the creation, update and reading of PowerPoint files. It is a very convenient tool for automating PPTX processing.
Installation
pip install python-pptx
Create
from pptx import Presentation ppt = Presentation() ppt.save('demo.pptx')
Open, save as
from pptx import Presentation ppt = Presentation('demo.pptx') ppt.save('new_demo.pptx')
Add slideshow
from pptx import Presentation ppt = Presentation() for i in range(11): layout = ppt.slide_layouts[i] slide = ppt.slides.add_slide(layout) ppt.save('Add slides.pptx')
Add title
from pptx import Presentation ppt = Presentation() layout = ppt.slide_layouts[0] slide = ppt.slides.add_slide(layout) title = slide.shapes.title title.text = 'Presentation production automation' subtitle = slide.placeholders[1] subtitle.text = 'python-pptx library from entry to proficiency' ppt.save('Add title 1.pptx')
from pptx import Presentation ppt = Presentation() layout = ppt.slide_layouts[1] slide = ppt.slides.add_slide(layout) title_shape = slide.shapes.title title_shape.text = 'Basic operations of python-pptx library' body_shape = slide.shapes.placeholders[1] tf = body_shape.text_frame p = tf.add_paragraph() p.text = 'Basic operations for presentations' p.level = 0 p = tf.add_paragraph() p.text = 'Create presentation' p.level = 1 p = tf.add_paragraph() p.text = 'Open and save presentation' p.level = 1 p = tf.add_paragraph() p.text = 'Open presentation' p.level = 2 p = tf.add_paragraph() p.text = 'Save presentation' p.level = 2 ppt.save('Add title 2.pptx')
Add text box
from pptx import Presentation from pptx.util import Cm ppt = Presentation() layout = ppt.slide_layouts[6] slide = ppt.slides.add_slide(layout) left = top = Cm(3) width = height = Cm(10) txBox = slide.shapes.add_textbox(left, top, width, height) tf = txBox.text_frame tf.text = 'Friends are as close as we are to each other. ' ppt.save('Add text box.pptx')
Format text
from pptx import Presentation from pptx.util import Cm, Pt from pptx.dml.color import RGBColor from pptx.enum.dml import MSO_THEME_COLOR from pptx.enum.text import MSO_ANCHOR, PP_ALIGN # Create a presentation and add slides ppt = Presentation() layout = ppt.slide_layouts[6] slide = ppt.slides.add_slide(layout) left, top, width, height = Cm(2.7), Cm(2), Cm(20), Cm(15) #Add a text box and format the text area txBox = slide.shapes.add_textbox(left, top, width, height) text_frame = txBox.text_frame text_frame.margin_top = Cm(0.5) text_frame.vertical_anchor = MSO_ANCHOR.TOP text_frame.word_wrap = True # Add the first paragraph of text and set the paragraph format and font format text_frame.clear() text_frame.text = 'Computer Science Classics' p = text_frame.paragraphs[0] p.alignment = PP_ALIGN.CENTER p.space_after = Pt(18) r = p.runs[0] r.font.name = 'Fangzheng Li changed to Simplified Chinese' r.font.size = Pt(42) r.font.bold = True r.font.color.theme_color = MSO_THEME_COLOR.ACCENT_1 #Add a second paragraph and format the paragraph p = text_frame.add_paragraph() p.alignment = PP_ALIGN.JUSTIFY p.line_spacing = 1.5 # Add the first text fragment in the second paragraph and set the font format r = p.add_run() r.text = 'Introduction to Algorithms' r.font.name = 'Times New Roman' r.font.size = Pt(26) r.font.italic=True r.font.bold = True r.font.color.rgb = RGBColor(255, 0, 0) # Add the 2nd text fragment in the 2nd paragraph and set the font format r = p.add_run() r.text = ', the Chinese translation is "Introduction to Algorithms". The language of this book is easy to understand and is very suitable for self-study. ' r.font.name = 'Fangzhengzhunyasong_GBK' r.font.size = Pt(26) r.font.italic = False r.font.bold = False r.font.color.rgb = RGBColor(0, 0, 0) ppt.save('Set text format.pptx')
Add image
from pptx import Presentation from pptx.util import Cm ppt = Presentation() layout = ppt.slide_layouts[5] slide = ppt.slides.add_slide(layout) slide.shapes.title.text = 'Fireworks in Yangzhou in March' image_file = 'watercolor.jpg' left = Cm(2.7) top = Cm(4) width = Cm(20) pic = slide.shapes.add_picture(image_file, left, top, width) ppt.save('Add picture.pptx')
Add shape
from pptx import Presentation from pptx.enum.shapes import MSO_SHAPE from pptx.util import Cm ppt = Presentation() layout = ppt.slide_layouts[5] slide = ppt.slides.add_slide(layout) slide.shapes.title.text = 'Add Shape' left = top = Cm(4) width = height = Cm(6) shape = slide.shapes.add_shape(MSO_SHAPE.SUN, left, top, width, height) shape.text = 'sun' ppt.save('Add shape.pptx')
Shape fill color
from pptx import Presentation from pptx.enum.shapes import MSO_SHAPE from pptx.enum.dml import MSO_THEME_COLOR from pptx.util import Cm ppt = Presentation() layout = ppt.slide_layouts[5] slide = ppt.slides.add_slide(layout) slide.shapes.title.text = 'Add shape' left, top, width, height = Cm(0.8), Cm(4), Cm(5), Cm(2.5) for n in range(1, 6): shape = slide.shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height) shape.text = f'Step {<!-- -->n}' fill = shape.fill fill.solid() fill.fore_color.theme_color = MSO_THEME_COLOR.ACCENT_1 fill.fore_color.brightness = -0.1 * n left = left + width - Cm(0.3) ppt.save('Fill the shape with color.pptx')
Add table
from pptx import Presentation from pptx.util import Cm ppt = Presentation() layout = ppt.slide_layouts[5] slide = ppt.slides.add_slide(layout) slide.shapes.title.text = 'Add table' #Add table rows = 5 cols = 4 left = Cm(2.7) top = Cm(4) width = Cm(20) height = Cm(6) table = slide.shapes.add_table(rows, cols, left, top, width, height).table # Set column width table.columns[0].width = Cm(6) table.columns[1].width = Cm(5) table.columns[2].width = Cm(4.5) table.columns[3].width = Cm(4.5) # Fill in the header table.cell(0, 0).text = 'Financial type' table.cell(0, 1).text = 'Big category' table.cell(0, 2).text = 'Amount (10,000 yuan)' table.cell(0, 3).text = 'Proportion' # Fill cells table.cell(1, 0).text = 'Asset' table.cell(1, 1).text = 'Long-term assets' table.cell(1, 2).text = '5645' table.cell(1, 3).text = '61.24%' table.cell(2, 1).text = 'Current assets' table.cell(2, 2).text = '3573' table.cell(2, 3).text = '38.76%' table.cell(3, 0).text = 'Liability' table.cell(3, 1).text = 'Shareholders' Equity' table.cell(3, 2).text = '3400' table.cell(3, 3).text = '48.76%' table.cell(4, 1).text = 'Current liabilities' table.cell(4, 2).text = '3573' table.cell(4, 3).text = '51.24%' # Merge Cells cell1 = table.cell(1, 0) cell1.merge(table.cell(2, 0)) cell2 = table.cell(3, 0) cell2.merge(table.cell(4, 0)) #Adjust padding cell1.margin_top = Cm(0.75) cell1.margin_left = Cm(2.5) cell2.margin_top = Cm(0.75) cell2.margin_left = Cm(2.5) ppt.save('Add table.pptx')
Add chart
from pptx import Presentation from pptx.chart.data import CategoryChartData from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION from pptx.util import Cm # Create a slideshow ppt = Presentation() layout = ppt.slide_layouts[5] slide = ppt.slides.add_slide(layout) slide.shapes.title.text = 'Number of people taking the exam in each branch' # Define chart data chart_data = CategoryChartData() chart_data.categories = ['Beijing', 'Shanghai', 'Shenzhen'] chart_data.add_series('Male', (19, 21, 16)) chart_data.add_series('Female', (27, 15, 20)) #Add chart to slide left, top, width, height = Cm(2.7), Cm(4), Cm(20), Cm(12) chart = slide.shapes.add_chart(XL_CHART_TYPE.COLUMN_CLUSTERED, left, top, width, height, chart_data).chart chart.has_legend = True chart.legend.position = XL_LEGEND_POSITION.RIGHT chart.legend.include_in_layout = False ppt.save('Add chart.pptx')
Extract text
from pptx import Presentation ppt = Presentation('Extract text.pptx') text_runs = [] for slide in ppt.slides: for shape in slide.shapes: if shape.has_text_frame: for paragraph in shape.text_frame.paragraphs: for run in paragraph.runs: text_runs.append(run.text) text_runs = '\ '.join(text_runs) with open('Extract text.txt', mode='w', encoding='utf-8') as f: f.write(text_runs)
Add notes
from pptx import Presentation ppt = Presentation('Add notes.pptx') for slide in ppt.slides: if not slide.has_notes_slide: notes_slide = slide.notes_slide text_frame = notes_slide.notes_text_frame text_frame.text = 'Pay attention to control the speaking speed and time' ppt.save('Add notes 1.pptx')
Made based on template
import pptx from pptx import Presentation from pptx.dml.color import RGBColor from pptx.util import Cm, Pt from pptx.enum.text import MSO_ANCHOR, PP_ALIGN ppt = Presentation('template.pptx') # Create title slide layout = ppt.slide_layouts[0] slide = ppt.slides.add_slide(layout) slide.shapes.placeholders[0].text = 'Personal Work Display' slide.shapes.placeholders[1].text = 'January 2022' # Create page 1 text layout = ppt.slide_layouts[1] slide = ppt.slides.add_slide(layout) slide.shapes.placeholders[0].text = 'Practical case study of learning Python web crawler from scratch\ Detailed explanation of the whole process (entry and improvement)' slide.shapes.placeholders[1].text = 'Price: 99.00 yuan\ ISBN: 978-7-111-68368-1\ Publication date: July 2021' left, top, width, height = Cm(1.3), Cm(10.2), Cm(15), Cm(7) content_box = slide.shapes.add_textbox(left, top, width, height) text_frame = content_box.text_frame text_frame.vertical_anchor = MSO_ANCHOR.TOP text_frame.word_wrap = True p = text_frame.paragraphs[0] p.alignment = PP_ALIGN.JUSTIFY r = p.add_run() r.text = 'Web crawlers are an indispensable and important means of obtaining data today. This book explains the basic knowledge and necessary skills of crawlers, and can help readers with no basic knowledge get started quickly and become proficient in using crawlers. ' r.font.name = 'Fangzheng Lanting Fine Black_GBK' r.font.size = Pt(24) r.font.color.rgb = RGBColor(0, 0, 0) image_file = 'Cover 1.png' left, top, height = Cm(18), Cm(4.3), Cm(13.5) slide.shapes.add_picture(image_file=image_file, left=left, top=top, height=height) # Create page 2 text layout = ppt.slide_layouts[1] slide = ppt.slides.add_slide(layout) slide.shapes.placeholders[0].text = 'Learn Python web crawler case from zero basis\ Detailed explanation of the whole process (advanced)' slide.shapes.placeholders[1].text = 'Price: 89.80 yuan\ ISBN: 978-7-111-68474-9\ Publication date: July 2021' left, top, width, height = Cm(1.3), Cm(10.2), Cm(15), Cm(7) content_box = slide.shapes.add_textbox(left, top, width, height) text_frame = content_box.text_frame text_frame.vertical_anchor = MSO_ANCHOR.TOP text_frame.word_wrap = True p = text_frame.paragraphs[0] p.alignment = PP_ALIGN.JUSTIFY r = p.add_run() r.text = 'Web crawlers are an indispensable and important means of obtaining data today. This book explains the advanced theories and techniques of crawlers, which can help readers further improve their practical application level. ' r.font.name = 'Fangzheng Lanting Fine Black_GBK' r.font.size = Pt(24) r.font.color.rgb = RGBColor(0, 0, 0) image_file = 'Cover 2.png' left, top, height = Cm(18), Cm(4.3), Cm(13.5) slide.shapes.add_picture(image_file=image_file, left=left, top=top, height=height) # Create ending slide layout = ppt.slide_layouts[2] slide = ppt.slides.add_slide(layout) slide.shapes.placeholders[0].text = 'Thanks for your attention' ppt.save('work display.pptx')
Reference
https://python-pptx.readthedocs.io/en/latest/index.html