Skip to content

Instantly share code, notes, and snippets.

@pankaj28843
Created August 23, 2014 10:24
Show Gist options
  • Save pankaj28843/26bd961cdb9901459520 to your computer and use it in GitHub Desktop.
Save pankaj28843/26bd961cdb9901459520 to your computer and use it in GitHub Desktop.
import sys
from zipfile import ZipFile
from StringIO import StringIO
from PyPDF2 import PdfFileReader, PdfFileWriter
def burst_pages_of_pdf_file(pdf_file):
zipped_output = StringIO()
with ZipFile(zipped_output, 'w') as ziparchive:
pdf_file.seek(0)
pdfreader = PdfFileReader(pdf_file)
for index in range(pdfreader.numPages):
pdfwriter = PdfFileWriter()
pdfwriter.addPage(pdfreader.getPage(index))
temp_filename = "Page {}.pdf".format(index + 1)
temp_file = StringIO()
pdfwriter.write(temp_file)
ziparchive.writestr(temp_filename, temp_file.getvalue())
return zipped_output
if __name__ == "__main__":
input_filepath = sys.argv[1]
output_filepath = sys.argv[2]
with open(input_filepath, 'rb') as input_file:
zipped_output = burst_pages_of_pdf_file(input_file)
with open(output_filepath, 'wb') as output_file:
output_file.write(zipped_output.getvalue())
@muj2022
Copy link

muj2022 commented Aug 23, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment