The code to compress PDF in Python

High-performance Python library to optimize PDF content and size retaining the original quality and baseline data

This is a professional solution to compress PDF using Python. Use our PDF optimization API to develop high-level, platform-independent software in Python. Try it online for free!

View code snippet

Compress PDF in Python

This Python library provides developers with the ability to quickly and efficiently compress PDF documents programmatically. Easily get optimized PDF output ready for printing, archiving or sharing over the network. It is a standalone PDF document processing solution for Python that does not require any third party software to be installed.

Reduce the size of a PDF using Python

Our product provides Python developers with an integrated API to reduce the size of PDF files. PDF compression is performed in two passes. During the first pass, all pages of the PDF are converted to image format. Our product uses adaptive and highly efficient image compression algorithms that allow developers to get compact PDF output while maintaining the optimal quality of the document. On the next pass, the sequence of images is compressed and combined to form the content of a new, optimized PDF file.

PDF compression made easy with 'Aspose.Words for Python via .NET'. The following example demonstrates how reduce the size of a PDF file in Python:

Code example in Python to compress PDF files
Upload a file you want to optimize/compress
Run code
Select the target format from the list
50%
pip install aspose-words
Copy
import aspose.words as aw

renderer = aw.pdf2word.fixedformats.PdfFixedRenderer()
pdf_read_options = aw.pdf2word.fixedformats.PdfFixedOptions()
pdf_read_options.image_format = aw.pdf2word.fixedformats.FixedImageFormat.JPEG
pdf_read_options.jpeg_quality = 50

with open ("Input.pdf", 'rb') as pdf_stream:
    pages_stream = renderer.save_pdf_as_images(pdf_stream, pdf_read_options);

builder = aw.DocumentBuilder()
for i in range(0, len(pages_stream)):
    # Set maximum page size to avoid the current page image scaling.
    max_page_dimension = 1584
    page_setup = builder.page_setup
    set_page_size(page_setup, max_page_dimension, max_page_dimension)

    page_image = builder.insert_image(pages_stream[i])

    set_page_size(page_setup, page_image.width, page_image.height)
    page_setup.top_margin = 0
    page_setup.left_margin = 0
    page_setup.bottom_margin = 0
    page_setup.right_margin = 0

    if i != len(pages_stream) - 1:
        builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE)

save_options = aw.saving.PdfSaveOptions()
save_options.cache_background_graphics = true

builder.document.save("Output.pdf", save_options)

def set_page_size(page_setup, width, height):

    page_setup.page_width = width;
    page_setup.page_height = height;
import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # It's up to developer to choose the library for image compression. image = Image.open(shape.image_data.to_stream()) # ... # Compress image and set it back to the shape. shape.image_data.set_image("yourCompressedImage") save_options = aw.saving.PdfSaveOptions save_options.cache_background_graphics = true doc.save("Output.pdf", save_options) import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # It's up to developer to choose the library for image compression. image = Image.open(shape.image_data.to_stream()) # ... # Compress image and set it back to the shape. shape.image_data.set_image("yourCompressedImage") doc.save("Output.pdf") import aspose.words as aw doc = aw.Document("Input.pdf") doc.cleanup() shapes = [node.as_shape() for node in doc.get_child_nodes(aw.NodeType.SHAPE, True)] for shape in shapes: if shape.isImage: # It's up to developer to choose the library for image compression. image = Image.open(shape.image_data.to_stream()) # ... # Compress image and set it back to the shape. shape.image_data.set_image("yourCompressedImage") save_options = aw.saving.OoxmlSaveOptions save_options.compression_level = aw.saving.CompressionLevel.MAXIMUM doc.save("Output.pdf", save_options) import aspose.words as aw doc = aw.Document() builder = aw.DocumentBuilder(doc) shape = builder.InsertImage("Input.pdf") save_options = aw.saving.ImageSaveOptions(aw.SaveFormat.PDF) shape.get_shape_renderer().save("Output.pdf", save_options) import aspose.words as aw renderer = aw.pdf2word.fixedformats.PdfFixedRenderer() pdf_read_options = aw.pdf2word.fixedformats.PdfFixedOptions() pdf_read_options.image_format = aw.pdf2word.fixedformats.FixedImageFormat.JPEG pdf_read_options.jpeg_quality = 50 with open ("Input.pdf", 'rb') as pdf_stream: pages_stream = renderer.save_pdf_as_images(pdf_stream, pdf_read_options); builder = aw.DocumentBuilder() for i in range(0, len(pages_stream)): # Set maximum page size to avoid the current page image scaling. max_page_dimension = 1584 page_setup = builder.page_setup set_page_size(page_setup, max_page_dimension, max_page_dimension) page_image = builder.insert_image(pages_stream[i]) set_page_size(page_setup, page_image.width, page_image.height) page_setup.top_margin = 0 page_setup.left_margin = 0 page_setup.bottom_margin = 0 page_setup.right_margin = 0 if i != len(pages_stream) - 1: builder.insert_break(aw.BreakType.SECTION_BREAK_NEW_PAGE) save_options = aw.saving.PdfSaveOptions() save_options.cache_background_graphics = true builder.document.save("Output.pdf", save_options) def set_page_size(page_setup, width, height): page_setup.page_width = width; page_setup.page_height = height;
Run code

How to compress PDF in Python

  1. Install Aspose.Words for Python via .NET.
  2. Add a library reference (import the library) to your Python project.
  3. Open the source PDF file in Python.
  4. Compress PDF without losing quality.
  5. Call the document_cleanup() method. The resulting code may vary depending on the output file format.
  6. Get the compressed result as a PDF file.

Python library to optimize and compress PDF files

We host our Python packages in PyPi repositories. Please follow the step-by-step instructions on how to install "Aspose.Words for Python via .NET" to your developer environment.

System Requirements

This package is compatible with Python ≥3.5 and <3.12. If you develop software for Linux, please have a look at additional requirements for gcc and libpython in Product Documentation.

Other supported file format optimizations

You can optimize files in many other file formats:

5%

Subscribe to Aspose Product Updates

Get monthly newsletters and offers directly delivered to your mailbox.

© Aspose Pty Ltd 2001-2024. All Rights Reserved.