简体   繁体   English

将多个 matplotlib 图形写入 PDF 文件

[英]Writing multiple matplotlib figures to PDF file

I have a list of Matplotlib figures that are saved as bytes in pdf format and base64 encoded.我有一个 Matplotlib 数字列表,这些数字以 pdf 格式和 base64 编码的字节保存。 I wish to save these figures to a PDF file with the following code:我希望使用以下代码将这些数字保存到 PDF 文件中:

with open('result.pdf', 'wb') as f:
    for fig in f_list:
        f.write(base64.b64decode(fig))

The file is created successfully but it seems like only the last figure is saved to the file.该文件已成功创建,但似乎只有最后一个数字保存到文件中。 What am I doing wrong??我究竟做错了什么??

All the figures are being written to the file but only the last page was being displayed.所有数字都被写入文件,但只显示最后一页。 I had to use a PyPDF2 to save and display all the pages.我不得不使用PyPDF2来保存和显示所有页面。

The implementation is below:实现如下:

from PyPDF2 import PdfFileReader, PdfFileWriter


writer = PdfFileWriter()
for fig in f_list:
    decoded_data = base64.b64decode(fig)
    reader = PdfFileReader(io.BytesIO(decoded_data)) # convert the figure to a stream object
    writer.addPage(reader.getPage(0))

with open('result.pdf', 'wb') as pdf_file:
    writer.write(pdf_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM