Compress PDF using Python

PDF compression programmatically using Aspose.PDF for Python for .NET

Optimize PDF with Python

Do you need to compress the PDF? Programmatic modification of PDF documents is an essential part of modern digital workflows. Python libraries like Aspose.PDF – are standalone solutions that don’t rely on other software and are ready for commercial use. They cover all possible needs of professional Python developers. This Python for .NET library enables developers to compress PDF documents quickly and efficiently through programming. With this library, you can easily produce optimized PDF output for printing, archiving, or network sharing. It is a standalone solution for processing PDF documents in Python for .NET that does not require the installation of any third-party software. To compress PDF files, we’ll use Aspose.PDF for Python via .NET, a feature-rich, powerful, easy-to-use document manipulation API for Python apps. Please open the website PiPy.org and install it. You may also use the following command from the Console or Terminal.

Console

pip install aspose-pdf

How to Compress PDF using Python


You need Aspose.PDF for Python via .NET to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Call the method for optimization on the document object, which removes unnecessary objects and compresses images in the PDF file
  3. Save the optimized document to the new path using the Save() method.

If you want to make it easier and faster to share or store a PDF file, compressing it can help. With this powerful Aspose.PDF Python for .NET library, you can reduce the size of your PDF while keeping its quality and important details intact. This professional tool uses Python to compress PDF effectively. The following example demonstrates how to modify a PDF document in Python:

Compress PDF Files - Python

This sample code shows how to Optimize PDF Document for the Web - Python

Input file:

File not added

Output format:

PDF

Output file:

import aspose.pdf as apdf

from os import path

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

document = apdf.Document(path_infile)
document.optimize()
document.save(path_outfile)

About Aspose.PDF for Python for .NET API

Aspose.PDF for Python via .NET API supports most established PDF standards and PDF specifications. It allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to compress PDF documents. Aspose.PDF for Python via .NET provides excellent security features for developing secure PDF documents. Some of the key features of Aspose.PDF for Python via .NET API include:

  • Ability to read & export PDF into multiple image formats, including BMP, GIF, JPEG & PNG.
  • Set basic information (e.g. author, creator) of the PDF document.
  • Conversion Features: Convert PDF to Word, Excel, and PowerPoint. Convert PDF to Image formats. Convert PDF files to HTML format and vice versa. Convert PDF to EPUB, Text, XPS, etc.

On API use, you can find more information about Aspose.PDF for Python via .NET API on our documentation.