【发布时间】:2014-12-24 05:23:44
【问题描述】:
按照代码示例here,我有下面的代码应该使用Ghostscript 压缩目录中的文件。代码压缩了文件,但是当我打开文件时,我根本看不到任何内容。我只想压缩文件。我不想删除内容。
from __future__ import print_function
import os
import subprocess
for root, dirs, files in os.walk("C:\comp"):
for file in files:
if file.endswith(".pdf"):
filename = os.path.join(root, file)
print (filename)
arg1= '-sOutputFile=' + file
p = subprocess.Popen(['C:/Program Files/gs/gs9.15/bin/gswin64c.exe',
'-sDEVICE=pdfwrite',
'-dCompatibilityLevel=1.4',
'-dPDFSETTINGS=/screen', '-dNOPAUSE',
'-dBATCH', '-dQUIET', str(arg1), filename],
stdout=subprocess.PIPE)
print (p.communicate())
【问题讨论】:
-
在 Windows 控制台的命令提示符下手动运行进程是否得到相同的结果?如果你看不到任何内容,你怎么知道它对文件做了什么?
-
pdfwrite 设备不会“压缩”PDF 文件,它会生成全新的文件。在我看来,您正在使输入和输出文件名相同,您不能 这样做。当您使输入和输出相同时,它所做的第一件事就是打开一个具有目标名称的文件,覆盖原始文件,并导致一个 0 字节的输出文件。注意我不会说 Python,所以我可能会误会,如果您引用实际的 Ghostscript 命令行,对我来说会更明显。
-
@KenS 感谢您对更改文件名的建议。我这样做了,它奏效了。非常感谢
标签: python pdf python-3.x compression ghostscript