Skip to content

Instantly share code, notes, and snippets.

@tangcheng2014
Forked from zhangchunlin/batch_qpdf.py
Created April 11, 2018 02:19
Show Gist options
  • Save tangcheng2014/7c713a4f86288bb267c675578acc8ef6 to your computer and use it in GitHub Desktop.
Save tangcheng2014/7c713a4f86288bb267c675578acc8ef6 to your computer and use it in GitHub Desktop.
batch_qpdf.py
#!/usr/bin/env python
#coding=utf-8
from __future__ import print_function
import os
import sys
def main():
dpath_in = os.path.expanduser(r"~/1del/")
dpath_out = os.path.expanduser(r"~/1delt/")
#download from https://github.com/qpdf/qpdf/releases
qpdf = os.path.expanduser(r"~/bin/qpdf")
pwd = "123456"
for dpath, dnames, fnames in os.walk(dpath_in):
for fname in fnames:
if fname.lower().endswith(".pdf"):
fpath = os.path.join(dpath,fname)
reldpath = os.path.relpath(dpath,dpath_in)
outdpath = os.path.join(dpath_out,reldpath)
outfpath = os.path.join(outdpath,fname)
if not os.path.exists(outdpath):
print("make new dir: %s"%(outdpath))
os.makedirs(outdpath)
cmd = "%s --password='%s' --decrype '%s' '%s'"%(qpdf,pwd,fpath,outfpath)
print(cmd)
ret = os.system(cmd)
if sys.platform=="linux2":
ret = ret>>8
if ret!=0:
print("error: return %s"%(ret))
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment