我下载了一个 pdf 文件,其中所有其他页面都是空白的,我想删除空白页。我可以在 pdf 工具(Adobe Acrobat、Preview.app、PDFPen 等)中手动执行此操作,但由于它有数百页,我希望执行更自动化的操作。有没有办法在 python 中做到这一点?
一种方法是使用 pypdf,因此首先在您的终端中执行
pip install pypdf4
然后创建一个与此类似的 .py 脚本文件:
# pdf_strip_every_other_page.py
from PyPDF4 import PdfFileReader, PdfFileWriter
number_of_pages = 500
output_writer = PdfFileWriter()
with open("/path/to/original.pdf", "rb") as inputfile:
pdfOne = PdfFileReader(inputfile)
for i in list(range(0, number_of_pages)):
if i % 2 == 0:
page = pdfOne.getPage(i)
output_writer.addPage(page)
with open("/path/to/output.pdf", "wb") as outfile:
output_writer.write(outfile)
Run Code Online (Sandbox Code Playgroud)
注意:您需要将路径更改为适合您的场景的路径。
显然,这个脚本相当粗糙,可以改进,但希望将其分享给其他想要快速处理这种情况的人。
| 归档时间: |
|
| 查看次数: |
1132 次 |
| 最近记录: |