Last active
March 16, 2019 00:02
-
-
Save artikrh/af6c73dc071c006148910f2fc9bf2c2f to your computer and use it in GitHub Desktop.
Split multiple-page PDFs to single-page ones
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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