安装模块
pip install pikepdf
代码
#!/usr/bin/python
import os
import sys
import pikepdf
def unlock_file(file):
pdf = pikepdf.open(file, allow_overwriting_input=True)
pdf.save(file)
def unlock_directory(folder = os.path.curdir):
print("Processing the folder: ", folder)
itemlist = os.listdir(folder)
print("Contents in the folder", folder, ": ", itemlist)
print()
for item in itemlist:
item = os.path.join(folder, item)
if os.path.isdir(item) :
print("processing the folder: ", item)
unlock_directory(item)
if os.path.isfile(item):
print("processing the file: ", item)
if os.path.splitext(item)[1] == '.pdf':
unlock_file(item)
if __name__ == '__main__':
if len(sys.argv) == 1:
unlock_directory()
else:
target = sys.argv[1]
if (os.path.isdir(target)):
print("Processing the folder: ", target)
unlock_directory(target)
else:
print("Processing the file: ", target)
unlock_file(target)
print('done')
使用说明
python pdf.py 目录名称
使用说明:
python pdf.py ./