压缩目录中所有pdf文件的脚本

编程入门 行业动态 更新时间:2024-10-23 07:39:06
本文介绍了压缩目录中所有pdf文件的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望使用ghostscript将目录中的所有pdf文件压缩. 我想到了使用python读取文件 压缩pdf的gs命令是

I wish to compress all pdf files in a directory using ghostscript. I thought of using python to read files and the gs command that compress pdf is

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

这会在"/screen"处显示语法错误, 对于下面的单个文件,命令可以正常工作

This gives syntax error at "/screen", For a single file below command works fine

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=output.pdf input.pdf

有人可以帮助我更正此脚本或替代解决方案吗?

Could someone help me correcting this script or alternate solution?

推荐答案

感谢您的建议tdelaney我尝试了subprocess.call,它有所帮助.下面是解决问题的代码.

Thanks for suggestion tdelaney I tried subprocess.call which helped. Below is the code solved the problem.

from __future__ import print_function import os import subprocess for root, dirs, files in os.walk("."): for file in files: if file.endswith(".pdf"): filename = os.path.join(root, file) arg1= '-sOutputFile=' +"v"+ file p = subprocess.Popen(['/usr/bin/gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-dPDFSETTINGS=/screen', '-dNOPAUSE', '-dBATCH', '-dQUIET', str(arg1), filename], stdout=subprocess.PIPE) print (pmunicate())

也可以找到... -exec是shell脚本的不错选择.

Also find ... -exec is good option for shell script though.

更多推荐

压缩目录中所有pdf文件的脚本

本文发布于:2023-11-01 20:20:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1550317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   文件   目录中   pdf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!