Reputation: 4849
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!
Upvotes: 2
Views: 2610
Reputation: 29031
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.
Upvotes: 3