Reputation: 2307
I would like to append pages to an existing pdf file.
Currently, I am using matplotlib pdfpages. however, once the file is closed, saving another figure into it overwrites the existing file rather than appending.
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
class plotClass(object):
def __init__(self):
self.PdfFile='c:/test.pdf'
self.foo1()
self.foo2()
def foo1(self):
plt.bar(1,1)
pdf = PdfPages(self.PdfFile)
pdf.savefig()
pdf.close()
def foo2(self):
plt.bar(1,2)
pdf = PdfPages(self.PdfFile)
pdf.savefig()
pdf.close()
test=plotClass()
I know appending is possible via multiple calls to pdf.savefig() before calling pdf.close() but I would like to append to pdf that has already been closed.
Alternatives to matplotlib would be appreciated also.
Upvotes: 11
Views: 19067
Reputation: 339120
You may want to use pyPdf for this.
# Merge two PDFs
from PyPDF2 import PdfFileReader, PdfFileWriter
output = PdfFileWriter()
pdfOne = PdfFileReader(open("path/to/pdf1.pdf", "rb"))
pdfTwo = PdfFileReader(open("path/to/pdf2.pdf", "rb"))
output.addPage(pdfOne.getPage(0))
output.addPage(pdfTwo.getPage(0))
outputStream = open(r"output.pdf", "wb")
output.write(outputStream)
outputStream.close()
Thereby you detach the plotting from the pdf-merging.
Upvotes: 8
Reputation: 11460
I searched around for a while but could not find a way to append to the same pdf file after re-opening it elsewhere in the program. I ended up using dictionaries, that way I can store the figures to the dictionary for each pdf I'm interested in creating and write them to pdfs at the end. Here is an example:
dd = defaultdict(list) #create a default dictionary
plot1 = df1.plot(kind='barh',stacked='True') #create a plot
dd[var].append(plot1.figure) #add figure to dictionary
#elsewhere in the program
plot2 = df2.plot(kind='barh',stacked='True') #another plot
dd[var].append(plot2.figure) #add figure to dictionary
#at the end print the figures to various reports
for var in dd.keys():
pdf = PdfPages(var+.'pdf') #for each dictionary create a new pdf doc
for figure in dd[k]:
pdf.savefig(figure) #write the figures for that dictionary
pdf.close()
Upvotes: 2