2

I create a pptx file using python pptx package (python 2.7)

I have a image in memory that i create using matplotlib with this code:

import matplotlib.pyplot as plt

plt.clf()

plt.imshow(data, cmap='hot', interpolation='nearest')
plt.xlabel(x_l)
plt.ylabel(y_l)
plt.title(title)

x_values = range(len(times))

plt.xticks(x_values[::len(x_values)/24], range(24))

plt.yticks(range(len(dates)),dates)
plt.axes().set_aspect('auto')

return plt.gcf()

Later i try to add this image to the pptx file using

pic = shapes.add_picture(image, left,top = top, width= width, height = height )

and get the following error: AttributeError: 'Figure' object has no attribute 'read'

The code works if i save the image to a file and then read it to the pptx file with the path, with exactly the same code. adding image file to pptx file

I found only how to add an image file to the pptx, and nothing about adding image from memory.

I need to add it without saving any file to disk.

Thanks!

1 Answer 1

3

You can save it to an in-memory StringIO stream (a "file-like object") and feed the StringIO object to python-pptx.

import StringIO

image_stream = StringIO.StringIO()
image.save(image_stream)
pic = shapes.add_picture(image_stream, left, top, width, height)
image_stream.close() # probably optional, but couldn't hurt

These other two questions have additional details.

Sign up to request clarification or add additional context in comments.

2 Comments

could you please detail the answer so i will be able to accept it?
@captainshai I've added a more explicit code snippet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.