I'm trying to write a function which splits a pdf into separate pages. From this SO answer. I copied a simple function which splits a pdf into separate pages:
def splitPdf(file_):
pdf = PdfFileReader(file_)
pages = []
for i in range(pdf.getNumPages()):
output = PdfFileWriter()
output.addPage(pdf.getPage(i))
with open("document-page%s.pdf" % i, "wb") as outputStream:
output.write(outputStream)
return pages
This however, writes the new PDFs to file, instead of returning a list of the new PDFs as file variables. So I changed the line of output.write(outputStream)
to:
pages.append(outputStream)
When trying to write the elements in the pages list however, I get a ValueError: I/O operation on closed file
.
Does anybody know how I can add the new files to the list and return them, instead of writing them to file? All tips are welcome!
pages.append(outputStream.read())
? – NunocStringIO.StringIO
to openoutputStream
? – JaquesdalcrozeStringIO
object for a file and get the result out as a string that way – GalacticIOError: File not open for reading
on the line sayingpages.append(outputStream.read())
. Any other ideas? – Ambrogino