Introduction to python-pptx
Introduction to python-pptx¶
What is python-pptx:A way to let us to make automated ppt from python package.
Getting start:To install, use:
pip install python-pptx
Import packages
from pptx import Presentationfrom pptx.util import Pt,Inchesfrom pptx.enum.text import PP_ALIGNimport matplotlib.pyplot as pltfrom pptx.dml.color import RGBColorfrom pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZEfrom pptx.oxml.xmlchemy import OxmlElement
Create presentation:Presentation() is where everything starts.So if you like a blank presentation, use below:
prs = Presentation() #create blank presentation
But sometimes we like some predefined format, so you can assign to your file path by:
prs = Presentation('some_example.pptx')
then you can use save to check current status:
prs.save('test.pptx')
We use prs to save Presentation object, next big thing is to create new page in prs.
Create page:In a pptx, you can use “View” > “slide Master” to check what default slide had been createWhen you hover over the slide, you can see some name of the slide.
When we find name of the slide we want to use, we can use below function to assign new page by name.
def get_layout(ppt): """ Key will be layout name and value to be layout use below to assign slide_layout = layout_hash['Title Slide 1'] """ layout_hash = {} for layout in ppt.slide_layouts: layout_hash[layout.name] = layout return layout_hash#create new slideslide = prs.slides.add_slide(ppt_layout['Slide name from pptx'])
Above functions will get a hash table where key is slide name and value is the module.Now you can add slide by below, remember to replace “Slide name from pptx” to your slide name.
Create shapes:Think shapes as some smaller stuff inside your slide, most common I’ll use is add_picture(): Adds an image to the slide.add_table(): Adds a table to the slide.add_textbox(): Adds a textbox to the slide.
add_textbox()
For example, you want to add a new textbox, you need to assign the 4 parameters to identify the shape of textbox.
then assign to parameter new_paragraph1, it’s easier to modify other objects inside.
left, top, width, height = Inches(6), Inches(5), Inches(5), Inches(10)new_paragraph1 = slide.shapes.add_textbox(left=left, top=top, width=width, height=height).text_frame# add more decoration belownew_paragraph1.paragraphs[0].text # the text insidenew_paragraph1.paragraphs[0].font.size # size of worldnew_paragraph1.paragraphs[0].font.bold # whether to bold or notnew_paragraph1.paragraphs[0].font.name # type of text you want to use, ex:arial(body)new_paragraph1.word_wrap # whether enable word wrapnew_paragraph1.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE # Enable auto-fit
And it would be easier just change these to a functions, so we can just input a list to assign
def add_content(ppt, setting:list): """ Sequence should be [slides, text_inside, font_size, font_bold, font_name, left, top, width, height] """ if len(setting)!=9: raise ValueError('The setting list should have 9 elements.') #Add content settings slide = setting[0] left, top, width, height = Inches(setting[5]), Inches(setting[6]), Inches(setting[7]), Inches(setting[8]) new_paragraph1 = slide.shapes.add_textbox(left=left, top=top, width=width, height=height).text_frame new_paragraph1.paragraphs[0].text = setting[1] new_paragraph1.paragraphs[0].font.size = Pt(setting[2]) new_paragraph1.paragraphs[0].font.bold = setting[3] new_paragraph1.paragraphs[0].font.name = setting[4] new_paragraph1.word_wrap = True # Enable word wrap new_paragraph1.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE # Enable auto-fit
add_picture()
You can add new picture in your ppt, for picture you can generate by matplotlib for some data plotting.Common parameter for picture is less, you also need to assign the 4 location parameter and the path to your image.
pic = slide.shapes.add_picture(img_path, left=left, top=top, width=width, height=height)
add_table
We also need tables! we can use
shapes.add_table(rows, cols, left, top, width, height).table
rows and cols is table shapes.And it you want to change the alignment, size, color, line spacing, you need to go by each cell, use table.cell(row, col) to assign the which cell your’re editing. Below is some example:
table = page_content.shapes.add_table(rows, cols, Inches(0.88), Inches(1.45), Inches(10.15), Inches(0.32*rows)).table#change size to 16for row in range(rows): for col in range(cols): cell = table.cell(row, col) cell.fill.solid() if row==0: #set first row to blue cell.fill.fore_color.rgb = RGBColor(37, 71, 247) # Set background color to blue else: cell.fill.fore_color.rgb = RGBColor(245, 245, 245) # Set background color to white if col==0 and row!=0: # all col in 0 exclude header to set spacing to 2 show to middle paragraph = cell.text_frame.paragraphs[0] paragraph.line_spacing = 2 for paragraph in cell.text_frame.paragraphs: paragraph.alignment = PP_ALIGN.CENTER # Center text horizontally for run in paragraph.runs: run.font.size = Pt(14) # Set font size to 16 points
So above is some common use functions in python-pptx.
You can also referring to https://python-pptx.readthedocs.io/en/latest/user/quickstart.html for some quick start.
Comments
Loading comments…
Leave a Comment