Updated answer which relies on pdftk.exe
only, without invoking Ghostscript
The answer provided by user @mmj used to work fine for me, but somehow ceased working somewhere between GS versions 9.20 and 9.50. I'm also aware of the solution provided by @Adobe. However, I like to get recurring tasks done from Windows (10) Explorer by selecting one or more files and going for right click → Send To. Here's a Python script (compatible to 3.8) that uses pdftk.exe
(tested with 2.02) to count the total number of pages and extract all to single files. It should accept multiple PDFs as input. Make sure you have Python and pdftk.exe
within PATH.
Name this extract-pdf-pages-py.cmd
and put it to shell:sendto
:
python %APPDATA%\Microsoft\Windows\SendTo\extract-pdf-pages-py.py %*
Put the following to extract-pdf-pages-py.py
in the same folder:
#!/usr/bin/python3
# put as extract-pdf-pages-py.py to shell:sendto
import os
import subprocess
import re
import sys
import mimetypes
def is_tool(name):
from shutil import which
return which(name) is not None
if not is_tool('pdftk'):
input('pdftk.exe not within PATH. Aborting...')
raise SystemExit("pdftk.exe not within PATH.")
sys.argv.pop(0)
for j in range(len(sys.argv)):
input_pdf = sys.argv[j]
if 'application/pdf' not in mimetypes.guess_type(input_pdf):
input(f"File {input_pdf} is not a PDF. Skipping...")
continue
savefile = input_pdf.rstrip('.pdf')
numpages = subprocess.Popen(f"pdftk \"{input_pdf}\" dump_data", shell=True, stdout=subprocess.PIPE)
output1 = str(numpages.communicate()[0])
output2 = re.search("NumberOfPages: ([0-9]*)", output1)
number_of_pages = int(output2.group(1))
for i in range(1, number_of_pages + 1):
os.system(f"pdftk \"{input_pdf}\" cat {i} output \"{savefile}\"{i:04d}.pdf")
I've used code from this answer (script by @Adobe) and that one (is_tool
).