Library Documentation¶
For normal use, all you will have to do to get started is:
from pypyt import *
This will import the following functions:
Template Functions
- The
open_template()
function opens a template. - The
render_template()
function to renders a template. - The
render_and_save_template()
function renders and saves a template.
Presentation Functions:
- The
get_shapes()
function gets all the shapes and its type in a presentation. - The
get_shapes_by_name()
function gets all the shapes with the given name in a presentation. - The
render_ppt()
function renders a presentation. - The
render_and_save_ppt()
function renders and save a presentation. - The
save_ppt()
function saves a presentation.
Shape Functions:
- The
get_shape_type()
function gets the type of a shape. - The
is_chart()
function returnsTrue
if the given shape is a chart. - The
is_hyperlink()
function returnsTrue
if the given shape is a hyperlink. - The
is_paragraph()
function returnsTrue
if the given shape is a paragraph. - The
is_picture()
function returnsTrue
if the given shape is a picture. - The
is_table()
function returnsTrue
if the given shape is a table.
Render Functions:
- The
render_chart()
function renders a chart object. - The
render_hyperlink()
function renders a hyperlink object. - The
render_paragraph()
function renders a paragraph object. - The
render_picture()
function renders a picture object. - The
render_table()
function renders a table object.
Utility Functions:
- The
picture_from_url()
function gets an image from the given url.
Documentation Functions:
- The
pypyt_doc()
function opens this documentation.
Template Functions¶
-
pypyt.
open_template
(template_name)¶ Opens a pptx file given the template_name and returns it.
Parameters: template_name (str) – The name of the file to be open. Returns: Presentation object Return type: pptx.presentation.Presentation Examples
Open a ppt template for later use
>>> open_template('template.pptx') <pptx.presentation.Presentation at ...>
-
pypyt.
render_template
(template_name, values)¶ Returns a rendered presentation given the template name and values to be rendered.
Parameters: - template_name (str) – Name of the presentation to be rendered.
- values (dict) – Dictionary with the values to render on the template.
- raise_error (bool) – Boolean that indicates whether an error has to be raised or not when is not possible to render a shape
Returns: Rendered presentation
Return type: pptx.presentation.Presentation
Examples
Render a template.
>>> values = {'presentation_title': "My Cool Presentation"} >>> render_template('template.pptx', values) <pptx.presentation.Presentation at ...>
-
pypyt.
render_and_save_template
(template_name, values, filename)¶ Renders and save a presentation with the given template name, values to be rendered, and filename.
Parameters: - template_name (str) – Name of the presentation to be saved.
- values (dict) – Dictionary with the values to render on the template.
- raise_error (bool) – Boolean that indicates whether an error has to be raised or not when is not possible to render a shape
- filename (str) – Name of the file to be saved.
Examples
Render and save a template
>>> values = {'presentation_title': "My Cool Presentation"} >>> render_and_save_template('template.pptx', values, 'presentation.pptx')
Presentation Functions¶
-
pypyt.
get_shapes
(prs)¶ Returns a dictionary to be filled with the values to be rendered in the presentation.
Parameters: - prs (pptx.presentation.Presentation) – Presentation to get the shapes from.
- get_all (bool) – If False, filters out the shapes that are believed to be created automatically
Returns: Dictionary with the shape names as keys and an empty data structure to fill the values.
Return type: dict
Examples
Get all the shapes in a presentation.
>>> prs = open_template('template.pptx') >>> get_shapes(prs) {'chart': {'categories': [], 'data': [], 'title': ''}, 'client_name': '', 'presentation_title': '', 'slide_text': {'cpc_change': '', 'year': ''}, 'slide_title': '', 'table': [[None, None, None], [None, None, None], [None, None, None]]}
>>> get_shapes(prs, all=True) {'Title 1': '', 'chart': {'categories': [], 'data': [], 'title': ''}, 'client_name': '', 'presentation_title': '', 'slide_text': {'cpc_change': '', 'year': ''}, 'slide_title': '', 'table': [[None, None, None], [None, None, None], [None, None, None]]}
-
pypyt.
get_shapes_by_name
(prs, name)¶ Returns a list of shapes with the given name in the given presentation.
Parameters: - prs (pptx.presentation.Presentation) – Presentation to be saved.
- name (str) – Name of the shape(s) to be returned.
Examples
Get all the shapes named ‘chart’ in a presentation.
>>> prs = open_template('template.pptx') >>> get_shapes_by_name(prs, 'chart') [<pptx.shapes.placeholder.PlaceholderGraphicFrame at ...>]
-
pypyt.
render_ppt
(prs, values)¶ Returns a rendered presentation given the template name and values to be rendered.
Parameters: - prs (pptx.presentation.Presentation) – Presentation to be rendered.
- values (dict) – Dictionary with the values to render on the template.
- raise_error (bool) – Boolean that indicates whether an error has to be raised or not when is not possible to render a shape
Returns: Rendered presentation
Return type: pptx.presentation.Presentation
Examples
Open a template and render it.
>>> prs = open_template('template.pptx') >>> values = {'presentation_title': "My Cool Presentation"} >>> render_ppt(prs, values) <pptx.presentation.Presentation at ...>
-
pypyt.
render_and_save_ppt
(template_name, values, filename)¶ Renders and save a presentation with the given template name, values to be rendered, and filename.
Parameters: - prs (Presentation) – Name of the presentation to be saved.
- values (dict) – Dictionary with the values to render on the template.
- filename (str) – Name of the file to be saved.
- raise_error (bool) – Boolean that indicates whether an error has to be raised or not when is not possible to render a shape
Examples
Open a template, render it and save it.
>>> values = {'presentation_title': "My Cool Presentation"} >>> prs = open_template('template.pptx') >>> render_and_save_ppt(prs, values, 'presentation.pptx')
-
pypyt.
save_ppt
(prs, filename)¶ Saves the given presentation with the given filename.
Parameters: - prs (pptx.presentation.Presentation) – Presentation to be saved.
- filename (str) – Name of the file to be saved.
Examples
Render a template and save it.
>>> values = {'presentation_title': "My Cool Presentation"} >>> rendered_prs = render_template('template.pptx', values) >>> save_ppt(rendered_prs, 'presentation.pptx')
Shape Functions¶
-
pypyt.
get_shape_type
(shape)¶ Returns a string with the kind of the given shape.
Parameters: shape (pptx.shapes.BaseShape) – Shape to get the type from. Returns: String representing the type of the shape Return type: string Examples
Get the type of a shape.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> get_shape_type(shapes[0]) 'paragraph'
-
pypyt.
is_chart
(shape)¶ Checks whether the given shape is a chart.
Parameters: shape (pptx.shapes.base.BaseShape) – Shape to get whether is a chart or not. Returns: Boolean representing whether the given shape is a table or no. Return type: bool Examples
Check whether the given shape is a table.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> is_chart(shapes[0]) True
-
pypyt.
is_hyperlink
(shape)¶ Checks whether the given shape is a hyperlink
Parameters: shape (pptx.shapes.base.BaseShape) – Shape to get whether is a hyperlink or not. Returns: Boolean representing whether the given shape is a hyperlink or not. Return type: bool Examples
Check whether the given shape is a hyperlink.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> is_hyperlink(shapes[0]) False
-
pypyt.
is_paragraph
(shape)¶ Checks whether the given shape is a paragraph.
Parameters: shape (pptx.shapes.base.BaseShape) – Shape to get whether is a paragraph or not. Returns: Boolean representing whether the given shape is a table or no. Return type: bool Examples
Check whether the given shape is a table.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> is_paragraph(shapes[0]) True
-
pypyt.
is_picture
(shape)¶ Checks whether the given shape is a picture.
Parameters: shape (pptx.shapes.base.BaseShape) – Shape to get whether is a picture or not. Returns: Boolean representing whether the given shape is a picture or not. Return type: bool Examples
Check whether the given shape is a picture.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> is_picture(shapes[0]) False
-
pypyt.
is_table
(shape)¶ Checks whether the given shape is a table.
Parameters: shape (pptx.shapes.base.BaseShape) – Shape to get whether is a table or not. Returns: Boolean representing whether the given shape is a table or no. Return type: bool Examples
Check whether the given shape is a table.
>>> prs = open_template('template.pptx') >>> shapes = get_shapes_by_name(prs, 'client_name') >>> is_table(shapes[0]) False
Render Functions¶
-
pypyt.
render_chart
(values, chart)¶ Renders the given values into the given chart.
Parameters: - values (dict or pandas.DataFrame) – Values to render the chart.
- chart (pptx.chart.chart.Chart) – Chart object to be rendered.
Examples
Render a chart from a dictionary
>>> prs = open_template('template.pptx') >>> chart_values = { ... 'title': "My Cool Graph", ... 'categories': ['d1', 'd2', 'd3'], ... 'data':{ ... 'displays': [500, 750, 600], ... 'clicks': [150, 250, 200] ... } ... } >>> shapes = get_shapes_by_name(prs, 'chart') >>> shape = shapes[0] >>> render_chart(chart_values, shape.chart)
Render a chart for a pandas DataFrame
>>> prs = open_template('template.pptx') >>> data = [ ... [250, 500], ... [150, 750], ... [350, 600], ... [300, 450], ... [175, 500], ... [275, 700], ... [125, 550], ... ] >>> pd_chart = pd.DataFrame(data, ... index=['day1', 'day2', 'day3', 'day4', 'day5', 'day6', 'day7'], ... columns=['clicks', 'displays']) >>> pd_chart clicks displays 0 250 500 1 150 750 2 350 600 3 300 500 4 175 500 5 275 700 6 125 550 >>> pd_chart.title = "Cool Graph" >>> shapes = get_shapes_by_name(prs, 'chart') >>> shape = shapes[0] >>> render_chart(pd_chart, shape.chart)
-
pypyt.
render_hyperlink
(values, hyperlink)¶ Renders the given values into the given hyperlink.
Parameters: - values (str) – Address of the link.
- hyperlink – Hyperlink object to be rendered.
Examples
Render a hyperlink
>>> prs = open_template('template.pptx') >>> hyperlink_value = { ... 'https://pypyt.readthedocs.io' ... } >>> shapes = get_shapes_by_name(prs, 'hyperlink') >>> shape = shapes[0] >>> render_chart(hyperlink_value, shape)
-
pypyt.
render_paragraph
(values, text_frame)¶ In the case you want to replace the whole text.
Parameters: - values (dict, str, int or float) – Values to render the text.
- text_frame (pptx.text.text.TextFrame) – TextFrame object to be rendered.
Examples
Replace full text.
>>> prs = open_template('template.pptx') >>> paragraph = { ... 'slide_title': "Cool insight", ... } >>> shapes = get_shapes_by_name(prs, 'slide_title') >>> shape = shapes[0] >>> render_table(paragraph, shape.text_frame)
Replace placeholders within text.
>>> prs = open_template('template.pptx') >>> paragraph = { ... 'slide_text': { ... 'year': 2018, ... 'cpc_change': 50 ... } ... } >>> shapes = get_shapes_by_name(prs, 'slide_text') >>> shape = shapes[0] >>> render_table(paragraph, shape.text_frame)
-
pypyt.
render_picture
(values, table)¶ Renders an image with the given values.
Parameters: - values (string (filename) or file-like object) – Reference to the image
- image (pptx.shapes.picture.Picture) – Shape where to render the image
Examples
Render image from image filename
>>> prs = open_template('template.pptx') >>> picture = 'image_file.jpg' >>> shapes = get_shapes_by_name(prs, 'image') >>> shape = shapes[0] >>> render_picture(picture, shape)
Render image from file-like object
>>> import io >>> prs = open_template('template.pptx') >>> with open('image_file.jpg', 'br') as file: >>> picture = io.BytesIO(file.read()) >>> shapes = get_shapes_by_name(prs, 'image') >>> shape = shapes[0] >>> render_picture(picture, shape)
-
pypyt.
render_table
(values, table)¶ Renders a table with the given values.
Parameters: - values (dict, list or pandas.DataFrame) – Values to render the table
- table (pptx.shapes.table.Table) – Table object to be rendered.
Examples
Render table from python list
>>> prs = open_template('template.pptx') >>> table: [ ... ['header1', 'header2', 'header3'], ... ['cell1', 'cell2', 'cell3'], ... ['cell4', 'cell5', 'cell6'] ... ] >>> shapes = get_shapes_by_name(prs, 'table') >>> shape = shapes[0] >>> render_table(table, shape.table)
Render table from pandas DataFrame without header
>>> prs = open_template('template.pptx') >>> data = [ ... ['header', 'header2', 'header3'], ... ['cell1', 'cell2', 'cell3'], ... ['cell4', 'cell5', 'cell6'] ... ] >>> table_df = pd.DataFrame(data) >>> table_df col1 col2 col3 0 header1 header2 header3 1 cell1 cell2 cell3 2 cell4 cell5 cell6 >>> shapes = get_shapes_by_name(prs, 'table') >>> shape = shapes[0] >>> render_chart(table_df, shape.table)
Render a table from a pandas DataFrame with header.
>>> prs = open_template('template.pptx') >>> data = [ ... ['cell1', 'cell2', 'cell3'], ... ['cell4', 'cell5', 'cell6'] ... ] >>> table_df = pd.DataFrame(data, columns=['header', 'header2', 'header3']) >>> table_df header1 header2 header3 0 cell1 cell2 cell3 1 cell4 cell5 cell6 >>> table_df.header = True >>> shapes = get_shapes_by_name(prs, 'table') >>> shape = shapes[0] >>> render_chart(table_df, shape.table)
Utility Functions¶
-
pypyt.
picture_from_url
(url)¶ Loads an image from a given url.
Parameters: url (string) – Url from the image Returns: Byte object with the image. Return type: io.BytesIO Examples
Render image from image url
>>> prs = open_template('template.pptx') >>> url = 'http://yoursite.com/image_file.jpg' >>> shapes = get_shapes_by_name(prs, 'image') >>> shape = shapes[0] >>> render_picture(picture_from_url(url), shape)