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()
Which results in a powerpoint slide which looks as follows:
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
Built Distribution
File details
Details for the file mplppt-0.0.5.tar.gz
.
File metadata
- Download URL: mplppt-0.0.5.tar.gz
- Upload date:
- Size: 32.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b85f6629e2f6a5b200a3c862acc78f8f9c1a7d783b0b2c0697547571afc2f40c |
|
MD5 | 6ebc6108018c0de3d9f9d78c2ba957f2 |
|
BLAKE2b-256 | 943e4ea8d79b2488d747f3b8767a2f22f09b06bca716d210c7b887d3cf03c330 |
File details
Details for the file mplppt-0.0.5-py3-none-any.whl
.
File metadata
- Download URL: mplppt-0.0.5-py3-none-any.whl
- Upload date:
- Size: 59.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cafcbb550115dd07fb6fc88efc3bca897d27cd1e90d310bda13986378ca9f620 |
|
MD5 | acd54a940aa71bde8d5e3c70acba7e79 |
|
BLAKE2b-256 | d5f7950db7106a69cd145ee023814c3e4609a0e9fc0a29537ab757982ee202e1 |