API 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_paragraph()
function returnsTrue
if the given shape is a paragraph. - The
is_table()
function returnsTrue
if the given shape is a table.
Render Functions:
- The
render_chart()
function renders a chart object. - The
render_paragraph()
function renders a paragraph object. - The
render_table()
function renders a table object.
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.
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.
- 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 tuple of tuples with the shape name and shape type.
Parameters: prs (pptx.presentation.Presentation) – Presentation to get the shapes from. Returns: Tuple with the shapes. Return type: tuple Examples
Get all the shapes in a presentation.
>>> prs = open_template('template.pptx') >>> get_shapes(prs) (('client_name', 'paragraph'), ('presentation_title', 'paragraph'), ('slide_text', 'paragraph'), ('slide_title', 'paragraph'), ('chart', 'chart'), ('Title 1', 'paragraph'), ('table', 'table'), ('Title 1', 'paragraph'))
-
pypyt.
get_shapes_by_name
(prs, name)¶ Returns a list of shapes with the given name in the given presentation. :param prs: Presentation to be saved. :type prs: pptx.presentation.Presentation :param name: Name of the shape(s) to be returned. :type name: str
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.
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: - template_name (str) – 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.
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_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_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_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_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)