Create Open XML PowerPoint documents in Python

Overview
https://travis-ci.org/scanny/python-pptx.svg?branch=master

python-pptx is a Python library for creating and updating PowerPoint (.pptx) files.

A typical use would be generating a customized PowerPoint presentation from database content, downloadable by clicking a link in a web application. Several developers have used it to automate production of presentation-ready engineering status reports based on information held in their work management system. It could also be used for making bulk updates to a library of presentations or simply to automate the production of a slide or two that would be tedious to get right by hand.

More information is available in the python-pptx documentation.

Browse examples with screenshots to get a quick idea what you can do with python-pptx.

Comments
  • feature: add table into Table placeholder shape

    feature: add table into Table placeholder shape

    Hi,

    I'm trying to do either: a) Have a table placeholder shape in my layout, and stuff a table into it - this doesn't seem to be possible as I can't set rows,cols etc on an existing table shape or b) Create a new table shape using the height,width,left,top etc from a placeholder shape in the layout. This seems to fail as the actual shape in the slide ends up with height,width,top,left all set to 0. The cloned placeholder shape seems to not copy these values into the local shape?

    Example:

    >>> for idx,ph in enumerate(ps.slidelayouts[3].shapes.placeholders):
    ...     print "id: %d name: %s height: %d sz: %r" % (idx, ph.name, ph.height, ph.sz)
    ... 
    id: 0 name: Title 1 height: 747338 sz: 'full'
    id: 1 name: Content Placeholder 2 height: 3098203 sz: 'half'
    id: 2 name: Content Placeholder 3 height: 3098203 sz: 'half'
    id: 3 name: Date Placeholder 4 height: 0 sz: 'half'
    id: 4 name: Footer Placeholder 5 height: 0 sz: 'quarter'
    id: 5 name: Slide Number Placeholder 6 height: 0 sz: 'quarter'
    id: 6 name: Table Placeholder 8 height: 1045863 sz: 'quarter'
    id: 7 name: Text Placeholder 10 height: 939800 sz: 'quarter'
    
    >>> for idx,ph in enumerate(sld.shapes.placeholders):
    ...     print "id: %d name: %s height: %d sz: %r" % (idx, ph.name, ph.height, ph.sz)
    ... 
    id: 0 name: Title 1 height: 0 sz: 'full'
    id: 1 name: Content Placeholder 2 height: 0 sz: 'half'
    id: 2 name: Content Placeholder 3 height: 0 sz: 'half'
    id: 3 name: Table Placeholder 4 height: 0 sz: 'quarter'
    id: 4 name: Text Placeholder 5 height: 0 sz: 'quarter'
    

    which makes my attempt to copy those values fall flat.

    Any ideas?

    Jess

    table 
    opened by castaway 22
  • python-pptx output cannot be opened in Keynote (resubmit of #66)

    python-pptx output cannot be opened in Keynote (resubmit of #66)

    I saw issue 66 and thought I would contribute some debugging files.

    I created a pptx from python-pptx and confirmed it wouldn't open in Keynote. Saved to another name in PowerPoint and this new file could be opened in Keynote. I installed opc-diag and generated the output.

    github n00b, so now I need to figure out how to upload the files.

    opened by tvanyo 21
  • Powerpoint needs to

    Powerpoint needs to "repair" the file?

    Hi Steve, this is an awesome library. I just started using it--apologies if my issue is simplistic or if I can find the answer elsewhere. I didn't see this in the docs or any previous issue--I am creating a pptx and serving it via Apache. I have the mimetype set to application/vnd.openxmlformats-officedocument.presentationml.presentation per one of your other issue comments. The file downloads fine from Chrome and FF, but when I open it, I get a "PowerPoint found a problem with content in the file...PowerPoint can attempt to repair the file. If you trust the source of this file, click Repair."

    It's just a simple ppt with text and some images. Googling identifies a problem with "block / unblock" for Windows, but even after I unblock the file on Windows, PowerPoint still has an issue and wants to repair it (Mac and Windows 7 VM).

    Have you seen this issue before? My Python code follows your examples. Thanks!

    opened by coleshaw 18
  • KeyError:

    KeyError: "no content type 'image/x-wmf' in pptx.spec.pml_parttypes"

    When I try to load a template I am getting this error. KeyError: "no content type 'image/x-wmf' in pptx.spec.pml_parttypes"

    I added the following snippet to the "pml_parttypes" dictioanary in the "pptx/spec.py" file and it seems to fix the problem. Not sure if its the right fix.

    'image/x-wmf': { 'basename' : 'image' , 'ext' : '.wmf' , 'name' : 'Image Part' , 'cardinality' : PTS_CARDINALITY_TUPLE , 'required' : False , 'baseURI' : '/ppt/media' , 'has_rels' : PTS_HASRELS_NEVER , 'rels_from' : ['handoutMaster', 'notesSlide', 'notesMaster', 'slide', 'slideLayout', 'slideMaster'] , 'reltype' : RT_IMAGE }

    opened by rkakarlapudi 18
  • how to set multi-axis for chart

    how to set multi-axis for chart

    Hi PPTX Team I am trying to study pptx module to output presentation for CAE, but I can’t find an example for setting multi-axis. Do you have example for it? Thanks a lot For example: I want to set a chart and contain three vertical axis and one horizontal axis. example

    opened by zhaowei699 15
  • feature: ignore package relationships pointing to

    feature: ignore package relationships pointing to "NULL"

    Code pres = Presentation('file.pptx')

    Relevant Error: File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1428, in read with self.open(name, "r", pwd) as fp: File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1467, in open zinfo = self.getinfo(name) File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1395, in getinfo 'There is no item named %r in the archive' % name) KeyError: "There is no item named 'ppt/slides/NULL' in the archive"

    opened by PuneethaPai 14
  • KeyError: no relationship of type

    KeyError: no relationship of type

    I'd like to extract the text content from ppt slides following the document:

    from pptx import Presentation
    
    prs = Presentation('notes/lec02.ppt')
    
    # text_runs will be populated with a list of strings,
    # one for each text run in presentation
    text_runs = []
    
    for slide in prs.slides:
        for shape in slide.shapes:
            if not shape.has_textframe:
                continue
            for paragraph in shape.textframe.paragraphs:
                for run in paragraph.runs:
                    text_runs.append(run.text)
    

    But got such a weird error:

    KeyError                                  Traceback (most recent call last)
    <ipython-input-2-5ce116f22745> in <module>()
          1 from pptx import Presentation
          2 
    ----> 3 prs = Presentation('notes/lec02.ppt')
          4 
          5 # text_runs will be populated with a list of strings,
    
    /Users/yobichi/bigdata/lib/python3.5/site-packages/pptx/api.py in Presentation(pptx)
         28         pptx = _default_pptx_path()
         29 
    ---> 30     presentation_part = Package.open(pptx).main_document_part
         31 
         32     if not _is_pptx_package(presentation_part):
    
    /Users/yobichi/bigdata/lib/python3.5/site-packages/pptx/opc/package.py in main_document_part(self)
         98         for a SpreadsheetML package.
         99         """
    --> 100         return self.part_related_by(RT.OFFICE_DOCUMENT)
        101 
        102     def next_partname(self, tmpl):
    
    /Users/yobichi/bigdata/lib/python3.5/site-packages/pptx/opc/package.py in part_related_by(self, reltype)
        131         if more than one such relationship is found.
        132         """
    --> 133         return self.rels.part_with_reltype(reltype)
        134 
        135     @property
    
    /Users/yobichi/bigdata/lib/python3.5/site-packages/pptx/opc/package.py in part_with_reltype(self, reltype)
        434         is found.
        435         """
    --> 436         rel = self._get_rel_of_type(reltype)
        437         return rel.target_part
        438 
    
    /Users/yobichi/bigdata/lib/python3.5/site-packages/pptx/opc/package.py in _get_rel_of_type(self, reltype)
        487         if len(matching) == 0:
        488             tmpl = "no relationship of type '%s' in collection"
    --> 489             raise KeyError(tmpl % reltype)
        490         if len(matching) > 1:
        491             tmpl = "multiple relationships of type '%s' in collection"
    
    KeyError: "no relationship of type 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' in collection"
    

    Did I do anything wrong? How should I deal with this issue? Thanks!

    opened by ghost 14
  • text direction: right to left

    text direction: right to left

    Within power point, under "paragraph", it is possible to set "Right-to-Left" text direction. How can it be done with python pptx? It isn't text alignment. Text alignment is something else. So if my presentation contains Hebrew then the text gets messed up. Thanks.

    opened by eladlavi 13
  • 'zipfile.BadZipFile' error in python 3.6 but not python2

    'zipfile.BadZipFile' error in python 3.6 but not python2

    This seems to be a similar issue to the one described here: https://github.com/scanny/python-pptx/issues/174

    I'm running a virtualenv with Python3.6. This is the error that I get when I see when I try to read in a powerpoint presentation using the ppt.Presentation() method:

      File "slidenator.py", line 309, in <module>
        create_charts(presentation_name)
      File "slidenator.py", line 183, in create_charts
        prs = ppt.Presentation(f)
      File "/Users/maria/Desktop/PowerpointPythonProject/python3-version/test/lib/python3.6/site-packages/pptx/api.py", line 30, in Presentation
        presentation_part = Package.open(pptx).main_document_part
      File "/Users/maria/Desktop/PowerpointPythonProject/python3-version/test/lib/python3.6/site-packages/pptx/opc/package.py", line 122, in open
        pkg_reader = PackageReader.from_file(pkg_file)
      File "/Users/maria/Desktop/PowerpointPythonProject/python3-version/test/lib/python3.6/site-packages/pptx/opc/pkgreader.py", line 32, in from_file
        phys_reader = PhysPkgReader(pkg_file)
      File "/Users/maria/Desktop/PowerpointPythonProject/python3-version/test/lib/python3.6/site-packages/pptx/opc/phys_pkg.py", line 102, in __init__
        self._zipf = ZipFile(pkg_file, 'r')
      File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 1108, in __init__
        self._RealGetContents()
      File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 1175, in _RealGetContents
        raise BadZipFile("File is not a zip file")
    zipfile.BadZipFile: File is not a zip file
    

    This used to work just fine in Python2. Not sure what the issue could be here. The solution described previously mentioned that there was some issue with argparse, but I'm unclear what that means.

    opened by mariashen 13
  • feature: DataLabels.show_category_name

    feature: DataLabels.show_category_name

    When creating a pie chart, is it possible to add the category names as labels to the chart eliminating the need for the legend ?

    chart.plots[0].has_data_labels = True data_labels = chart.plots[0].data_labels data_labels.number_format = '0%'

    This puts the series values as labels. It would be nice if we can put the category values also.

    chart 
    opened by ngnikhilgoyal 13
  • "Cannot identify image file" when giving file-like object

    I'm trying to add matplotlib plots to a PowerPoint. I have used this code on python-docx without issue. It doesn't seem to working for python-pptx.

    This code doesn't work and gives error OSError: cannot identify image file <_io.BytesIO object at 0x00000000407A17D8> It works fine if I have the file saved to file system and use a string path

    ...
    buf = io.BytesIO()
    t = list(range(0, 10, 1))
    plt.plot(t, t, 'r--', t, [x**2 for x in t], 'bs', t, [x**3 for x in t], 'g^')
    plt.savefig(buf, format="png")
    
    slide.placeholders[0].text = 'string1'
    slide.placeholders[13].text = 'string2'
    slide.placeholders[14].insert_picture(buf)
    

    Using this code in python-docx has worked fine on the same machine

    buff = io.BytesIO()
    t = list(range(0, 10, 1))
    plt.plot(t, t, 'r--', t, [x**2 for x in t], 'bs', t, [x**3 for x in t], 'g^')
    plt.savefig(buff, format="png")
    doc.add_picture(buff)
    

    Am I misunderstanding something about the API?

    shortlist documentation 
    opened by jkornblum 12
  • MSO_SHAPE not having rectangle shape

    MSO_SHAPE not having rectangle shape

    Hello, I am trying to execute the below code, but I am getting error as below. Please advise.

    shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))
    

    AttributeError: type object 'MSO_SHAPE_TYPE' has no attribute 'ROUNDED_RECTANGLE'

    #Import modules from pptx import Presentation from pptx.enum.shapes import MSO_SHAPE_TYPE as MSO_SHAPE #TYPE as MSO_SHAPE from pptx.util import Cm from pptx.enum.dml import MSO_THEME_COLOR_INDEX from pptx.util import Pt

    #Open powerpoint file prs = Presentation()

    #Create a slide slidelayout = prs.slide_layouts[0] slide = prs.slides.add_slide(slidelayout) shapes = slide.shapes

    #Add a shape shape = shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Cm(10), Cm(10), Cm(10), Cm(10))

    #Create a shadow shadow = shape.shadow shadow.inherit = False shadow.visible = True shadow.distance = Pt(10) shadow.shadow_type = 'outer' shadow.angle = 45 shadow.blur_radius = Pt(5) shadow.color = MSO_THEME_COLOR_INDEX.ACCENT_5 shadow.transparency = '50' shadow.distance = Pt(5) shape.shadow.style = 'outer'

    #Save the powerpoint file prs.save("D:/PPT/shadow.pptx")

    opened by Ms-Ajith 0
  • Google Slide masters and layout not understood

    Google Slide masters and layout not understood

    I have first modified a template using Google Slide. The file was originally created by PowerPoint. I modified the layout of the (only) master. It appears that PowerPoint can read these after exporting from GSlide, but python-pptx will read some default layouts, but cannot identify/read the modified ones. To summup: GSlide Masters and Layouts are not read but a default master+layout is applied instead.

    opened by JulienPez 0
  • Pptx on python 3.11

    Pptx on python 3.11

    Hello, I've installed new version of python 3.11, and wanted to install pptx module, but it seams there is some issue which I' cant solve. Not sure if pptx is available also within the new version of python ? image image

    opened by RomanHnizdiukh 2
  • Feature request: read/write access to animation delays

    Feature request: read/write access to animation delays

    I generate synthesized voice narrations for my slides and keep the text with time stamps, like so:

    (1.526)The (1.651)conditional (2.308)statement (2.839)consists (3.433)of (3.526)the (3.62)word (3.948)if, (4.526)a (4.62)condition, (5.576)the (5.686)colon, (6.542)and (6.683)the (6.761)body, (7.505)which (7.755)consists (8.317)of (8.411)the (8.505)statements (9.176)that (9.317)will (9.442)be (9.645)executed (10.348)if (10.458)the (10.551)condition (11.098)holds.

    Suppose I add a few words somewhere in the narration, which results in the timestamps being shifted. I want to be able to write a Python script to automatically shift the animation delays, so the animations would be synchronized with the modified narration.

    Hence, I would like to request a feature that would allow me to access and modify animation delays from a Python script. Thank you for the consideration.

    opened by mgoldenbe 0
Owner
Steve Canny
Steve Canny
xlwings is a BSD-licensed Python library that makes it easy to call Python from Excel and vice versa. It works with Microsoft Excel on Windows and macOS. Sign up for the newsletter or follow us on twitter via

xlwings - Make Excel fly with Python! xlwings CE xlwings CE is a BSD-licensed Python library that makes it easy to call Python from Excel and vice ver

xlwings 2.5k Jan 6, 2023
A Python module for creating Excel XLSX files.

XlsxWriter XlsxWriter is a Python module for writing files in the Excel 2007+ XLSX file format. XlsxWriter can be used to write text, numbers, formula

John McNamara 3.1k Dec 29, 2022
Python Module for Tabular Datasets in XLS, CSV, JSON, YAML, &c.

Tablib: format-agnostic tabular dataset library _____ ______ ___________ ______ __ /_______ ____ /_ ___ /___(_)___ /_ _ __/_ __ `/__ _

Jazzband 4.2k Dec 30, 2022
Xiaobo Zhang 30 Jan 8, 2023
Transpiler for Excel formula like language to Python. Support script and module mode

Transpiler for Excel formula like language to Python. Support script and module mode (formulas are functions).

Edward Villegas-Pulgarin 1 Dec 7, 2021
A set of Python scripts for finding threats in Office365

Py365 A collection of scripts for finding threats in Office365 Risky Rules A tool for finding risky or suspicious inbox rules - more detail in this po

Martin Rothe 49 May 18, 2022
Excel-report-evaluator - A simple Python GUI application to aid with bulk evaluation of Microsoft Excel reports.

Excel Report Evaluator Simple Python GUI with Tkinter for evaluating Microsoft Excel reports (.xlsx-Files). Usage Start main.py and choose one of the

Alexander H. 1 Dec 29, 2021
PyExcelerate - Accelerated Excel XLSX Writing Library for Python 2/3

PyExcelerate Accelerated Excel XLSX writing library for Python master: dev: test coverage: Authors: Kevin Wang and Kevin Zhang Copyright 2015 Kevin Wa

null 448 Dec 28, 2022
Play tic-tac-toe in PowerPoint

The presentation has around 6,000 slides representing every possible game state (and some impossible ones, since I didn't check for wins or ties). You play by clicking on the squares, which are hyperlinks to other slides.

Jesse Li 3 Dec 18, 2021
Parsel lets you extract data from XML/HTML documents using XPath or CSS selectors

Parsel Parsel is a BSD-licensed Python library to extract and remove data from HTML and XML using XPath and CSS selectors, optionally combined with re

Scrapy project 859 Dec 29, 2022
WeasyPrint is a smart solution helping web developers to create PDF documents.

WeasyPrint is a smart solution helping web developers to create PDF documents. It turns simple HTML pages into gorgeous statistical reports, invoices, tickets…

Kozea 5.4k Jan 8, 2023
PubMed Mapper: A Python library that map PubMed XML to Python object

pubmed-mapper: A Python Library that map PubMed XML to Python object 中文文档 1. Philosophy view UML Programmatically access PubMed article is a common ta

灵魂工具人 33 Dec 8, 2022
Converts XML to Python objects

untangle Documentation Converts XML to a Python object. Siblings with similar names are grouped into a list. Children can be accessed with parent.chil

Christian Stefanescu 567 Nov 30, 2022
Python module that makes working with XML feel like you are working with JSON

xmltodict xmltodict is a Python module that makes working with XML feel like you are working with JSON, as in this "spec": >>> print(json.dumps(xmltod

Martín Blech 5k Jan 4, 2023
The lxml XML toolkit for Python

What is lxml? lxml is the most feature-rich and easy-to-use library for processing XML and HTML in the Python language. It's also very fast and memory

null 2.3k Jan 2, 2023
A python script to convert an ucompressed Gnucash XML file to a text file for Ledger and hledger.

README 1 gnucash2ledger gnucash2ledger is a Python script based on the Github Gist by nonducor (nonducor/gcash2ledger.py). This Python script will tak

Thomas Freeman 0 Jan 28, 2022
Simple Python Library to convert JSON to XML

json2xml Simple Python Library to convert JSON to XML

Vinit Kumar 79 Nov 11, 2022
Python library for creating and parsing HSReplay XML files

python-hsreplay A python module for HSReplay support. https://hearthsim.info/hsreplay/ Installation The library is available on PyPI. pip install hsre

HearthSim 45 Mar 28, 2022
Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications.

Flask Sitemapper Flask Sitemapper is a small Python 3 package that generates XML sitemaps for Flask applications. This allows you to create a nice and

null 6 Jan 6, 2023
Safely add untrusted strings to HTML/XML markup.

MarkupSafe MarkupSafe implements a text object that escapes characters so it is safe to use in HTML and XML. Characters that have special meanings are

The Pallets Projects 514 Dec 31, 2022