Skip to content

Instantly share code, notes, and snippets.

@dzonesasaki
Created August 18, 2022 06:11
Show Gist options
  • Save dzonesasaki/cc780b91e4b5944796fc10bcea48ac27 to your computer and use it in GitHub Desktop.
Save dzonesasaki/cc780b91e4b5944796fc10bcea48ac27 to your computer and use it in GitHub Desktop.
splitting each pages for pdf file using PyPDF2
#!python3
#splitpdf.py
#ref to : https://fastclassinfo.com/entry/python_pdf_split/
import PyPDF2
import sys
fnameL = 'a.pdf'
if len(sys.argv)>1:
fnameL = sys.argv[1]
print(fnameL)
orgpdf = PyPDF2.PdfFileReader(fnameL)
fnameHead = fnameL[:-4]+'_page_'
for k in range(orgpdf.numPages):
apdf = PyPDF2.PdfFileWriter()
apdf.addPage(orgpdf.getPage(k))
fnames = fnameHead+'{0:03d}'.format(k)+'.pdf'
with open(fnames,'wb') as fid:
apdf.write(fid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment