我希望使用ghostscript压缩目录中的所有pdf文件。 我想用Python来读取文件 的和压缩PDF格式的GS命令脚本压缩目录中的所有pdf文件
from __future__ import print_function
import os
for path, dirs, files in os.walk("/home/mario/books"):
for file in files:
if file.endswith(".pdf"):
filename = os.path.join(root, file)
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=file filename
这给了语法错误,在“/屏幕”, 对于下面的命令单个文件工作正常
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=output.pdf input.pdf
有人可以帮我纠正这个脚本或备用解决方案吗?
你需要调用一个执行程序的python函数。查看'subprocess.call'。另外,在将文件名粘合在一起时,您使用的是错误的变量名,它应该是'filename = os.path.join(path,file)'。 – tdelaney 2014-11-24 18:59:56
['find ... -exec'](http://unixhelp.ed.ac.uk/CGI/man-cgi?find)可能是一个更简单的选项。 – georg 2014-11-24 19:15:06