【发布时间】:2020-01-06 14:40:47
【问题描述】:
我有一个大约 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)
【问题讨论】:
-
content变量应在循环外部/之上声明
标签: python python-3.x pdf pypdf