我有 pdf 文件,我在其中收到了来自 PyPDF2 的消息“不正确的 startxref 指针(1)”。 现在我想用 Ghostscript 和 Python 修复 pdf 文件。
我安装了:
pip 安装 ghostscript 幽灵脚本 10.01.1
但现在我迷路了。即使有了 Python-ghostscript 的示例,我现在也不知道如何开始。 我找到了语法
-o repaired.pdf ^
-sDEVICE=pdfwrite ^
-dPDFSETTINGS=/prepress ^
corrupted.pdf
有人可以帮助我吗?
import sys
from ghostscript import _gsprint as gs
args = [
b"gs", # actual value doesn't matter
b"-o repaired.pdf",
b"-sDEVICE=pdfwrite",
b"-dPDFSETTINGS=/press",
b"corrupted.pdf"
]
instance = gs.new_instance()
code = gs.init_with_args(instance, args)
code1 = gs.exit(instance)
if code == 0 or code == gs.e_Quit:
code = code1
gs.delete_instance(instance)
if not (code == 0 or code == gs.e_Quit):
sys.exit(1)
假设你已经安装了 GhostScript 并且在你的路径上,你不需要 Python 包装器。
也许像
import shutil
import subprocess
def repair_pdf(in_file, out_file):
gs = shutil.which("gs")
if not gs:
raise RuntimeError("Ghostscript not found")
subprocess.check_call(
[
gs,
"-dSAFER",
"-dNOPAUSE",
"-dBATCH",
"-sDEVICE=pdfwrite",
"-dPDFSETTINGS=/press",
"-o",
out_file,
in_file,
]
)
repair_pdf("corrupted.pdf", "repaired.pdf")