问题在于drawImage需要ImageReader对象或文件路径,而不是文件句柄。
以下应该工作:
import Imageimport matplotlib.pyplot as pltimport cStringIOfrom reportlab.pdfgen import canvasfrom reportlab.lib.units import inch, cmfrom reportlab.lib.utils import ImageReaderfig = plt.figure(figsize=(4, 3))plt.plot([1,2,3,4])plt.ylabel('some numbers')imgdata = cStringIO.StringIO()fig.savefig(imgdata, format='png')imgdata.seek(0) # rewind the dataImage = ImageReader(imgdata)c = canvas.Canvas('test.pdf')c.drawImage(Image, cm, cm, inch, inch)c.save()


