Skip to content

Instantly share code, notes, and snippets.

@hig3 hig3/distpdf.py
Created Sep 14, 2016

Embed
What would you like to do?
一定ページ数の複数文書が連結されたPDFファイルをpdftkで分割して, 標準入力からリストとして与えられたファイル名をつける Python スクリプト
# distpdf.py inname outname period start end docopy
# 0 =< start, end < periodb
# Example: distpdf.py in out 3 1 2 0
import sys
import os
import shutil
import re
markdir="./"
inname=sys.argv[1]
outname=sys.argv[2]
period=int(sys.argv[3])
start=int(sys.argv[4])
end=int(sys.argv[5])
docopy=int(sys.argv[6])
i=0
for line in sys.stdin:
if re.match("^#",line):
continue
if re.match("^$",line):
continue
tmp="pdftk " + inname + ".pdf"
tmp+=" cat %d-%d " %(i*period+1+start,i*period+1+end)
tmp+=" output " + markdir + outname + line[0:7] + ".pdf"
print (i+1),tmp
if docopy:
os.system(tmp)
i=i+1
tmp="pdftk " + inname + ".pdf"
tmp+=" cat %d-%d " %(i*period+1+start,i*period+1+end)
tmp+=" output " + outname + "_rest.pdf"
print (i+1),tmp
if docopy:
os.system(tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.