Skip to content

Instantly share code, notes, and snippets.

@artikrh
Last active March 16, 2019 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artikrh/af6c73dc071c006148910f2fc9bf2c2f to your computer and use it in GitHub Desktop.
Save artikrh/af6c73dc071c006148910f2fc9bf2c2f to your computer and use it in GitHub Desktop.
Split multiple-page PDFs to single-page ones
import argparse
from sys import exit
from PyPDF2 import PdfFileWriter, PdfFileReader
def main():
try:
inputpdf = PdfFileReader(open(args.file, "rb"))
except:
print("[*] The given file is not in the PDF format!")
exit()
for i in range(inputpdf.numPages):
output = PdfFileWriter()
output.addPage(inputpdf.getPage(i))
with open("%s-{}".format(args.file) % (i+1), "wb") as outputStream:
output.write(outputStream)
def usage():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', nargs="?", help="PDF File to split", required=True)
return parser.parse_args()
if __name__ == '__main__':
args = usage()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment