[英]Writing multiple matplotlib figures to PDF file
我有一個 Matplotlib 數字列表,這些數字以 pdf 格式和 base64 編碼的字節保存。 我希望使用以下代碼將這些數字保存到 PDF 文件中:
with open('result.pdf', 'wb') as f:
for fig in f_list:
f.write(base64.b64decode(fig))
該文件已成功創建,但似乎只有最后一個數字保存到文件中。 我究竟做錯了什么??
所有數字都被寫入文件,但只顯示最后一頁。 我不得不使用PyPDF2來保存和顯示所有頁面。
實現如下:
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.