使用 Python 压缩 PDF 或减小 PDF 文件大小的 5 种方法

liftword2个月前 (03-03)技术文章16


在存储、共享和传输方面,处理大型 PDF 文件可能是一个真正的挑战。PDF 压缩提供了一种有效的解决方案来减小文件大小,使文档更易于管理并优化存储使用情况。压缩的 PDF 文件具有几个关键优势:

  • 降低存储成本:较小的文件大小意味着较低的长期存储要求和费用。
  • 提高传输效率:减小的文件大小可以提高上传和下载速度,特别有利于通过电子邮件、云驱动器和其他渠道共享文件。
  • 增强的用户体验:轻量级 PDF 文件的加载和显示速度更快,尤其是在移动设备上,同时还可以减少网络带宽消耗。
  • 提高内容可访问性:紧凑的 PDF 文件更容易被搜索引擎索引和发现,有助于提高内容的可见性和覆盖范围。

用于压缩 PDF 文件的 Python 库

要在 Python 中压缩 PDF 文件,我们将使用 Spire.PDF for Python。它是一个功能丰富且用户友好的库,旨在在 Python 应用程序中创建、读取、编辑和转换 PDF 文件。

您可以使用以下 pip 命令从 PyPI 安装 Spire.PDF for Python:

pip install Spire.Pdf

如果您已经安装了 Spire.PDF for Python 并希望升级到最新版本,请使用以下 pip 命令:

pip install --upgrade Spire.Pdf

有关安装的更多详细信息,您可以查看此官方文档:如何在 VS Code 中安装 Spire.PDF for Python。

通过使用 Python 优化图像来压缩 PDF

PDF 文件通常包含大量高分辨率图像,这些图像可能是影响文件整体大小的主要因素。通过压缩这些图像并降低其分辨率,您可以显著减小 PDF 的文件大小。

Spire.PDF for Python 中的 PdfCompressor 类负责压缩 PDF 文件。通过使用
PdfCompressor.OptimizationOptions.SetImageQuality()、
PdfCompressor.OptimizationOptions.SetResizeImages()

PdfCompressor.OptimizationOptions.SetIsCompressImage()
方法,您可以通过优化其中包含的图像来轻松压缩 PDF 文件。

这是一个简单的示例,展示了如何通过使用 Python 优化图像来压缩 PDF:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfCompressor object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
compressor = PdfCompressor(input_pdf)

# Configure the compression options to optimize images in the PDF
compression_options = compressor.OptimizationOptions
compression_options.SetImageQuality(ImageQuality.Medium)
compression_options.SetResizeImages(True)
compression_options.SetIsCompressImage(True)

# Compress the PDF file and save the result to a new file
output_pdf = "OptimizingImages.pdf"
compressor.CompressToFile(output_pdf)

使用 Python 压缩 PDF 中的图像

通过使用 Python 优化字体来压缩 PDF

PDF 中嵌入的字体也会导致文件大小变大。通过压缩或取消嵌入来优化这些字体有助于减小文件大小。

要压缩或取消嵌入 PDF 文件中的字体,您可以使用
PdfCompressor.OptimizationOptions.SetIsCompressFonts()

PdfCompressor.OptimizationOptions.SetIsUnembedFonts()
方法。

下面是一个简单的示例,演示如何通过使用 Python 压缩或取消嵌入字体来压缩 PDF:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfCompressor object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
compressor = PdfCompressor(input_pdf)

# Configure the compression options to optimize fonts in the PDF
compression_options = compressor.OptimizationOptions
# Enable font compression
compression_options.SetIsCompressFonts(True)
# Or enable font unembedding
# compression_options.SetIsUnembedFonts(True)

# Compress the PDF file and save the result to a new file
output_pdf = "OptimizingFonts.pdf"
compressor.CompressToFile(output_pdf)

通过使用 Python 删除附件来压缩 PDF

PDF 文件有时可能包含附件,例如图像、文档或其他媒体。这些附加文件会显著增加 PDF 的整体大小。通过使用
PdfDocument.Attachments.Clear()
方法,您可以轻松地从 PDF 文件中删除所有附件。

这是一个简单的示例,展示了如何通过使用 Python 删除 PDF 中的所有附件来减小 PDF 的文件大小:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False

# Remove all attachments from the PDF
pdf.Attachments.Clear()

# Save the result to a new file
output_pdf = "RemovingAttachments.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()

通过使用 Python 删除或拼合表单域来压缩 PDF

与静态 PDF 文档相比,包含交互式表单域(如文本框、复选框或下拉菜单)的 PDF 文档往往具有更大的文件大小。这是因为表单域数据和关联的元数据单独存储在 PDF 文件中。通过完全删除表单域或拼合它们,您可以显著减小 PDF 的文件大小。

下面是一个简单的示例,演示如何通过使用 Python 拼合表单域来减小 PDF 的文件大小:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False

# Flatten the form fields in the PDF
pdf.Form.IsFlatten = True
    
# Save the result to a new file
output_pdf = "FlatteningForms.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()

下面是一个简单的示例,演示如何通过使用 Python 删除表单域来减小 PDF 的文件大小:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False

# Get the forms in the PDF
form = pdf.Form
formWidget = PdfFormWidget(form)

# Remove all forms from the PDF
formWidget.FieldsWidget.Clear()
    
# Save the result to a new file
output_pdf = "RemovingForms.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()

通过使用 Python 删除注释来压缩 PDF

PDF 文档可以包含各种类型的批注,如注释和高亮显示。这些批注在 PDF 文件中作为单独的对象存储,这可能会导致整体文件大小增加。通过从 PDF 中删除批注,您可以有效地减小文件大小,而不会影响文档的核心内容。

下面是一个简单的示例,演示如何通过使用 Python 删除批注来减小 PDF 的文件大小:

from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and specify the path of the PDF file to be compressed
input_pdf = "Sample.pdf"
pdf = PdfDocument(input_pdf)
# Disable the incremental update
pdf.FileInfo.IncrementalUpdate = False

# Remove all annotations from the pages of the PDF
for i in range(pdf.Pages.Count):
    page = pdf.Pages[i]
    page.Annotations.Clear()
    
# Save the result to a new file
output_pdf = "RemovingAnnotations.pdf"
pdf.SaveToFile(output_pdf)
pdf.Close()

相关文章

20 天学 Python 文件操作:Day 8 压缩与解压文件

在日常开发中,压缩和解压文件是处理文件操作的常见需求。Python 提供了丰富的内置模块,比如 zipfile 和 shutil,让我们能够高效地压缩和解压文件。今天我们将学习如何使用这些模块进行操作...

20 天学 Python 文件操作:Day 16 文件压缩与解压

在日常工作中,我们经常需要对文件进行压缩和解压操作以减少存储空间或方便文件传输。今天,我们将学习如何使用 Python 进行文件的压缩与解压。1. 使用 zipfile 模块进行 ZIP 文件操作创建...

Python全能压缩:ZIP的压缩、解压、文件筛选与删除,一键搞定!

引言:这个方法实现了文件压缩与解压的常见操作,涵盖内容如下:1、从文件夹创建 ZIP 文件 2、从文件夹创建 ZIP 文件(筛选特定文件) 3、解压 ZIP 文件中的所有内容 4、解压 ZIP...

python散装笔记——131: 解压文件

为了提取或解压缩tarball(tar归档文件)、ZIP文件或gzip文件,Python分别提供了tarfile、zipfile和gzip模块。Python的tarfile模块提供了TarFile.e...

15《Python 办公自动化教程》文件压缩与解压缩

压缩包也是我们平时工作中经常要接触到的文件格式,压缩文件后缀名通常有 .zip、.rar、.7z 等等。Python 中也有专门用来操作压缩包文件的第三方模块 zipfile。听这个名字就知道是用来操...