Package: python-pptx-interface
python-pptxis a great module to create pptx-files.
But it can be challenging to master the complex syntax. This module tries to present an easier interface
for python-pptx to create PowerPoint files. It also adds some still missing features like moving slides,
create links to other slides or remove unused place-holders. If matplotlib is installed, it can be used,
to write formulas with latex syntax. And if PowerPoint is available, a presentation could be exported as
pdf or png(s).Contentclass PPTXCreator: Create pptx-File from template, incluing methods to add text, tables, figures etc.class PPTXPosition: Allows positioning as fraction of slide height/width.Style sheetsclass PPTXFontStyle: Helps to set/change/copy fonts.class PPTXParagraphStyle: Format paragraphs (alignment, indent …).class PPTXTableStyle: Used to layout tables.class PPTXFillStyleWorking with templatesclass AbstractTemplate: Base class for all custom templates (enforce necessary attributes)class TemplateExample: Example class to show how to work with custom templatesutils.py: A collection of useful functions, eg. to generate PDF or PNG from *.pptx (needs PowerPoint installed)Examples: Collection of examples demonstrating how to use python-pptx-interface.Example: demonstrates usage of some key-features of python-pptx-interface with explanationsGeneral example 01: demonstrates usage of some key-features of python-pptx-interfaceFont style example 01Table style example 01class PPTXCreatorThis class provides an easy interface to create a PowerPoint presentation via python-pptx. It has methods to add slides
and shapes (tables, textboxes, matplotlib figures) setting format by using layouts and stylesheets. It also has methods
to move slides around, remove empty placeholders or create hyperlinks.Methods defined:add_content_slideAdd a content slide with hyperlinks to all other slides and puts it to position slide_index.add_latex_formulaAdd the given latex-like math-formula as an image to the presentation using matplotlib.add_matplotlib_figureAdd a motplotlib figure to slide and position it via position.
Optional parameter zoom sets image scaling in PowerPoint. Only used if width not in kwargs (default = 1.0).add_slideAdd a new slide to presentation. If no layout is given, default_layout is used.add_tableAdd a table shape with given table_data at position using table_style. (table_data: outer iter -> rows, inner iter cols; auto_merge: not implemented jet)add_text_boxAdd a text box with given text using given position and font. Uses self.default_position if no position is given.add_title_slideAdd a new slide to presentation. If no layout is given, title_layout is used.move_slideMove the given slide to position new_index.saveSave presentation under the given filename.save_as_pdfSave the presentation as pdf under the given filenmae. Needs PowerPoint installed.save_as_pngSaves the presentation as PNG’s in the given folder. Needs PowerPoint installed.Static methods defined:create_hyperlink(run: pptx.text.text._Run, shape: pptx.shapes.autoshape.Shape, to_slide: pptx.slide.Slide)Make the given run a hyperlink to to_slide.remove_unpopulated_shapes(slide: pptx.slide.Slide)Removes empty placeholders (e.g. due to layout) from slide. Further testing needed.Properties defined:prs: python-pptx Presentation objectslides: list of all slides in presentationtemplate: used template filetitle_layout: laxout used for title slidedefault_layout: default layoutdefault_position: used, when no PPTXPosition is given to add_table/add_text_box/… methodsclass PPTXPositionTo position shapes in a slide, many methods of PPTXCreator except a PPTXPosition parameter. It allows to give a position
relative to slide width and high (as a fraction). Additionally you can specify the position in inches starting from the
relative position. Some stylesheets e.g. PPTXTableStyle can also have an optional PPTXPosition attribute. In that case
writing the style to a shape will also set its position.Methods defined:dictReturns a kwargs dict containing “left” and “top”.tupleReturns an args tuple containing “left” and “top”.Properties defined:left: left position [inches] starting from rel_leftleft_rel: distance from slide left (relative to slide width)prs: python-pptx Presentation objecttop: top position [inches] starting from rel_toptop_rel: distance from slide top (relative to slide height)StylesheetsWhile python-pptx-interface can load a template file with placeholders, the intended use case is more focused on
creating and positioning shapes like tables, pictures, textboxes etc. directly in python. Therefore all unused
placeholders are removed by default, when creating a new slide. As it can be quite tedious to do all the necessary
formatting directly using python-pptx, this package provides some style sheet like classes, to define a certain format
and than “write” it to the created shapes. In general python-pptx-interface styles only change parameters, that
have been set. E.g. when creating a PPTXFontStyle instance and setting the font size, using this style will only
change the font size, but not color, bold … attributes. Beside setting an attribute or not changing an attribute
there is a third case - using the default value as it is defined e.g. in the master slide. For that case, the value_USE_DEFAULTcan be used.To be consistent, python-pptx-interface will not change anything if an attribute is set to None.
This can differ from the pyrhon-pptx behaviour in some cases, where None means “use default”.class PPTXFontStylefont-style exampleMethods defined:read_fontRead attributes from a pptx.text.text.Font object.setConvenience method to set several font attributes together.write_fontWrite attributes to a pptx.text.text.Font object.write_paragraphWrite attributes to given paragraph.write_runWrite attributes to given run.write_shapeWrite attributes to all paragraphs in given pptx.shapes.autoshape.Shape.
Raises TypeError if given shape has no text_frame or table.write_text_frameWrite attributes to all paragraphs in given text_frame.Properties defined:boldcapscolor_rgbfill_styleitalicunderlinesizestrikethroughclass PPTXParagraphStyleparagraph-style example,Methods defined:read_paragraphRead attributes from a _Paragraph object.setConvenience method to set several paragraph attributes together.write_paragraphWrite attributes to given paragraph.write_shapeWrite attributes to all paragraphs in given pptx.shapes.autoshape.Shape.
Raises TypeError if given shape has no text_frame or table.write_text_frameWrite attributes to all paragraphs in given text_frame.Properties defined:alignmentfont_stylelevelline_spacingspace_afterspace_beforeclass PPTXTableStyletable-style example,Methods defined:read_tableRead attributes from a Table object, ignoring font and cell style.setConvenience method to set several table attributes together.set_width_as_fractionSet table width as fraction of slide width.write_shape“””Write attributes to table in given pptx.shapes.autoshape.Shape.”””
Raises TypeError if given shape has no text_frame or table.write_table“””Write attributes to table object.”””Properties defined:cell_stylecol_bandingcol_ratiosfirst_row_headerfont_styleline_spacingpositionrow_bandingspace_afterspace_beforewidthclass PPTXFillStyle…Methods defined:setConvenience method to set several fill attributes together.write_fillWrite attributes to a FillFormat object.Properties defined:fill_typefore_color_rgbfore_color_mso_themefore_color_brightnessback_color_rgbback_color_mso_themeback_color_brightnesspatternWorking with templates…class AbstractTemplate…class TemplateExample…utils.py…ExamplesExampleThis module comes with ageneral example,
that you could run likefrompptx_tools.examplesimportgeneral_example_01asexampleexample.run(save_dir=my_dir)# you have to specify the folder where to save the presentationThis will create an example.pptx, using some of the key-features of python-pptx-interface. Lets have a closer look:frompptx_tools.creatorimportPPTXCreator,PPTXPositionfrompptx_tools.style_sheetsimportfont_title,font_defaultfrompptx_tools.templatesimportTemplateExamplefrompptx.enum.langimportMSO_LANGUAGE_IDfrompptx.enum.textimportMSO_TEXT_UNDERLINE_TYPEtry:importmatplotlib.pyplotaspltmatplotlib_installed=TrueexceptImportErrorase:matplotlib_installed=FalseFirst we need to import some stuff.PPTXCreatoris the class used to create a *.pptx file.PPTXPositionallows as to position shapes in more intuitive units of slide width/height.font_titleis a function returning a PPTXFontStyle instance. We will use it to change the formatting of the title shape.TemplateExampleis a class providing access to the example-template.pptx included in python-pptx-interface
and also setting some texts on the master slides like author, date and website. You could use it as reference
on how to use your own template files by subclassing AbstractTemplate
(you need at least to specify a path to your template and define a default_layout and a title_layout).MSO_LANGUAGE_IDis used to set the language of text andMSO_TEXT_UNDERLINE_TYPEis used to format underlining.Importing matplotlib is optional - it is used to demonstrate, how to get a matplotlib figure into your presentation.defrun(save_dir:str):pp=PPTXCreator(TemplateExample())PPTXFontStyle.lanaguage_id=MSO_LANGUAGE_ID.ENGLISH_UKPPTXFontStyle.name="Roboto"title_slide=pp.add_title_slide("Example presentation")font=font_title()# returns a PPTXFontStyle instance with bold font and size = 32 Ptfont.write_shape(title_slide.shapes.title)# change font attributes for all paragraphs in shapeNow we create our presentation withPPTXCreatorusing theTemplateExample.
We also set the default font language and name of allPPTXFontStyleinstances. This is not necessary,
asENGLISH_UKandRobotoare the defaults anyway. But in principle you could change these settings here,
to fit your needs. If you create your own template class, you might also set these default parameters there.
Finally we add a title slide and change the font style of the title using title_font().slide2=pp.add_slide("page2")pp.add_slide("page3")pp.add_slide("page4")content_slide=pp.add_content_slide()# add slide with hyperlinks to all other slidesNext, we add three slides, and create a content slide with hyperlinks to all other slides. By default,
it is put to the second position (you could specify the position using the optional slide_index parameter).Lets add some more stuff to the title slide.text="This text has three paragraphs. This is the first.\n"\"Das ist der zweite ...\n"\"... and the third."my_font=font_default()# font size 14my_font.size=16text_shape=pp.add_text_box(title_slide,text,PPTXPosition(0.02,0.24),my_font)PPTXCreator.add_text_box()places a new text shape on a slide with the given text.
Optionally it accepts a PPTXPosition and a PPTXFont. With PPTXPosition(0.02, 0.24)
we position the figure 0.02 slide widths from left and 0.24 slide heights from top.my_font.set(size=22,bold=True,language_id=MSO_LANGUAGE_ID.GERMAN)my_font.write_paragraph(text_shape.text_frame.paragraphs[1])my_font.set(size=18,bold=False,italic=True,name="Vivaldi",language_id=MSO_LANGUAGE_ID.ENGLISH_UK,underline=MSO_TEXT_UNDERLINE_TYPE.WAVY_DOUBLE_LINE)my_font.write_paragraph(text_shape.text_frame.paragraphs[2])We can use my_font to format individual paragraphs in a text_frame withPPTXFontStyle.write_paragraph().
ViaPPTXFontStyle.set()easily customize the font before using it.table_data=[]table_data.append([1,2])# rows can have different lengthtable_data.append([4,slide2,6])# there is specific type needed for entries (implemented as text=f"{entry}")table_data.append(["",8,9])pp.add_table(slide2,table_data)we can also easily add a table. First we define all the data we want to put in the table. Here we use a list of lists.
But add_table is more flexible and can work with anything, that is an Iterable of Iterable. The outer iterable defines,
how many rows the table will have. The longest inner iterable is used to get the number of columns.ifmatplotlib_installed:fig=create_demo_figure()pp.add_matplotlib_figure(fig,title_slide,PPTXPosition(0.3,0.4))pp.add_matplotlib_figure(fig,title_slide,PPTXPosition(0.3,0.4,fig.get_figwidth(),-1.0),zoom=0.4)pp.add_matplotlib_figure(fig,title_slide,PPTXPosition(0.3,0.4,fig.get_figwidth(),0.0),zoom=0.5)pp.add_matplotlib_figure(fig,title_slide,PPTXPosition(0.3,0.4,fig.get_figwidth(),1.5),zoom=0.6)If matplotlib is installed, we use it to create a demo figure, and add it to the title_slide.
With PPTXPosition(0.3, 0.4) we position the figure 0.3 slide widths from left and 0.4 slide heights from top.
PPTXPosition has two more optional parameters, to further position with inches values (starting from the relative position).pp.save(os.path.join(save_dir,"example.pptx"))Finally, we save the example as example.pptx.If you are on windows an have PowerPoint installed, you could use some additional features.try:# only on Windows with PowerPoint installed:filename_pptx=os.path.join(save_dir,"example.pptx")filename_pdf=os.path.join(save_dir,"example.pdf")foldername_png=os.path.join(save_dir,"example_pngs")# use absolute path, because its not clear where PowerPoint saves PDF/PNG ... otherwisepp.save(filename_pptx,create_pdf=True)# saves your pptx-file and also creates a PDF filepp.save_as_pdf(filename_pdf,overwrite=True)# saves presentation as PDFpp.save_as_png(foldername_png,overwrite_folder=True)# creates folder with PNGs of slidesexceptExceptionase:print(e)General example 01general example 01Font style example 01font style example 01Table style example 01table style example 01RequirementsPython >= 3.6 (f-strings)python-pptxOptional requirementsmatplotlib (adding matplotlib figures to presentation)comtypes  (create PDF’s or PNG’s)PowerPoint (create PDF’s or PNG’s)ContributionHelp with this project is welcome. You could report bugs or ask for improvements by creating a new issue.If you want to contribute code, here are some additional notes:This project uses 120 characters per line.Try to avoid abbreviations in names for functions or variables.Use type hints.Use Slide objects instead of IDs or index values as function parameter.