Mih*_*anu 4 python pdf pypdf python-3.x
我有一个大约 7000 页和 479 MB 的 PDF 文件。如果页面包含特定单词,我已经使用 PyPDF4 创建了一个 python 脚本来仅提取特定页面。该脚本有效,但新的 PDF 文件,尽管它只有原始 7000 页的 650 页,但现在比原始文件有更多的 MB(准确地说是 498 MB)。
有没有办法降低新PDF的文件大小?
我使用的脚本:
from PyPDF4 import PdfFileWriter, PdfFileReader
import os
import re
output = PdfFileWriter()
input = PdfFileReader(open('Binder.pdf', 'rb')) # open input
for i in range(0, input.getNumPages()):
content = ""
content += input.getPage(i).extractText() + "\n"
#Format 1
RS = re.search('FIGURE', content)
RS1 = #... Only one search given as example. I have more, but are irrelevant for the question.
#....
# Format 2
RS20 = re.search('FIG.', content)
RS21 = #... Only one search given as example. I have more, but are irrelevant for the question.
#....
if (all(v is not None for v in [RS, RS1, RS2, RS3, RS4, RS5, RS6, RS7, RS8, RS9]) or all(v is not None for v in [RS20, RS21, RS22, RS23, RS24, RS25, RS26, RS27, RS28, RS29, RS30, RS30])):
p = input.getPage(i)
output.addPage(p)
#Save pages to new PDF file
with open('ExtractedPages.pdf', 'wb') as f:
output.write(f)
Run Code Online (Sandbox Code Playgroud)
经过大量搜索找到了一些解决方案。导出的 PDF 文件的唯一问题是未压缩。所以我需要一个解决方案来压缩 PDF 文件:
PyPDF2 和/或 PyPDF4 没有压缩 PDF 的选项。PyPDF2 有compressContentStreams()方法,它不起作用。
找到了一些其他声称可以压缩 PDF 的解决方案,但没有一个对我有用(在此处添加它们以防万一它们对其他人有用): pylovepdf;pdfsizeopt ; pdf文件
第一个对我有用的解决方案是 Adobe Acrobat Professional。它将大小从 498 MB 减少到 2.99 MB。
[最佳解决方案] 作为替代的开源解决方案,我发现了concurrentpdf。对于 Windows,您可以下载预先构建的 PDF 挤压工具。然后在cmd中:
cpdfsqueeze.exe input.pdf output.pdf
这实际上比 Adobe Acrobat 更能压缩 PDF。从 498 MB 到 2.48 MB。从原始压缩到 0.5%。我认为这是最好的解决方案,因为它可以添加到您的 Python 代码中。
归档时间: |
|
查看次数: |
5412 次 |
最近记录: |