Skip to main content

Convert a matplotlib figure to a powerpoint slide

Project description

MPLPPT

mplppt is a simple library made from some hacky scripts I used to use to convert matplotlib figures to powerpoint figures. Which makes this a hacky library, I guess 😀.

Goal

mplppt seeks to implement an alternative savefig function for matplotlib figures. This savefig function saves a matplotlib figure with a single axis to a powerpoint presentation with a single slide containing the figure.

Installation

pip install mplppt

Supported Conversions

mplppt supports [partly] conversion of the following matplotlib objects:

  • Lines [matplotlib.lines.Line2D]
  • Rectangles [matplotlib.patches.Rectangle]
  • Polygons [matplotlib.patches.Polygon]
  • pcolormesh [matplotlib.collections.QuadMesh]
  • text [matplotlib.text.Text]

so far mplppt does not (yet) support (out of many other things):

  • markers (including tick marks)
  • linestyle

Simple Example

An example of all different conversions available for mplppt. Below we give an example of how all these objects can be combined into a single plot, which can then be exported to powerpoint:

# plot [Line2D]
x = np.linspace(-1,5)
y = np.sin(x)
plt.plot(x,y,color='C1')

# rectangle
plt.gca().add_patch(mpl.patches.Rectangle((0, 0), 3, 0.5))

# polygon
plt.gca().add_patch(mpl.patches.Polygon(np.array([[5.0,1.0],[4.0,-0.2],[2.0,0.6]]), color="red"))

# pcolormesh
x = np.linspace(0,1, 100)
y = np.linspace(0,1, 100)
X, Y = np.meshgrid(x,y)
Z = X**2 + Y**2
plt.pcolormesh(X,Y,Z)

# text
text = plt.text(0,0,'hello')

# set limits
plt.ylim(-0.5,1)

# Save figure to pptx
mplppt.savefig('first_example.pptx')

# show figure
plt.show()

png

Which results in a powerpoint slide which looks as follows:

simple powerpoint export screenshot

Cool! What else can I do with this?

You are not bound to using matplotlib! The mplppt repository contains some standard powerpoint shapes that you can use. Try something like:

ppt = mplppt.Group() # Create a new group of objects
ppt += mplppt.Rectangle(name='rect', x=0, y=0, cx=100, cy=100, slidesize=(10,5)) # add an object to the group
ppt.save('second_example.pptx') # export the group as a ppt slide

Is any of this documented?

No.

How does this work?

The repository contains a template folder, which is nothing more than an empty powerpoint presentation which is unzipped. After making a copy of the template folder and adding some xml code for the shapes, the modified folder is zipped into a .pptx file.

Copyright

© Floris Laporte - MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mplppt-0.0.3.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mplppt-0.0.3-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file mplppt-0.0.3.tar.gz.

File metadata

  • Download URL: mplppt-0.0.3.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mplppt-0.0.3.tar.gz
Algorithm Hash digest
SHA256 7a8c52d417292b48751279ed5a6ed98722ee59b620ce505483d2c6229ef2ac4a
MD5 967e0563e5220040728180968376bff6
BLAKE2b-256 12ae74bf56a3a8c0420a3ae99c60ac3486d20b74d2cafd2a03a51b155eb72c25

See more details on using hashes here.

File details

Details for the file mplppt-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: mplppt-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mplppt-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 f7a55f6bfae397eba78d27979623df5dbe0fa47a64dab0307be3e47650ca084a
MD5 591bbaa1448fbbfda4b20e4d681d074b
BLAKE2b-256 9305d6ac2daa9ab6735fae5ce15bbd141a93cbb8f75c59f5ac645659512c8f0f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page