Combine JPG to PDF in Python | Merge JPG Files to PDF

Combining multiple JPG images into a PDF document plays a vital role in various contexts, from creating photo albums to compiling documents for presentations and sharing with others. In this step-by-step guide with code examples, we will walk you through the entire process, from setting up your environment to executing the code. By the end of this article, you will have a solid understanding of how to combine JPG images into a PDF using Python.

This article covers the following topics:

  1. Python Library to Combine JPG to PDF
  2. Combine Multiple JPG Images to a PDF in Python
  3. Complete Code Example
  4. Combine JPG to PDF Online
  5. Free Resources

Python Library to Combine JPG to PDF

Aspose.PDF is a versatile library that allows developers to create, manipulate, and convert PDF files within their applications. It supports a wide range of features, including text manipulation, image insertion, and document merging. For combining JPG images into a PDF, we will use Aspose.PDF for Python. It enables Python developers to leverage these capabilities to work with PDF documents programmatically.

Setting Up the Environment

Before we dive into the code, let’s ensure we have the right environment set up for our project. For this purpose, we need:

  • Python 3.6 or higher
  • pip (Python package installer)
  • Aspose.PDF for Python library

Installing Aspose.PDF for Python

Please download the package from the downloads or install it from PyPI by running the following pip command in the console:

pip install aspose-pdf

This will download and install the Aspose.PDF library and its dependencies.

Combine Multiple JPG Images to a PDF in Python

We can easily combine multiple JPG images into a PDF document by following the steps below:

1. Load JPG Images from Directory

First, we need to load the JPG images that we want to merge into the PDF. We can use Python’s built-in libraries to read the images from the file system.

import os

# Directory containing JPG images
image_directory = "path/to/your/images"

# Get a list of all JPG files in the directory
image_files = [f for f in os.listdir(image_directory) if f.endswith('.jpg')]

2. Add Loaded Images to a PDF

Next, we will create a new PDF document and add each JPG image to a separate page. Aspose.PDF allows us to add images to PDF pages using the page.paragraphs.add() method.

# Create a PDF document and add the image to it
doc = Document()
page = doc.pages.add()

# Add images to the page
for img in image_files:
    # Create an image object
    image = Image()
    # Set the image file stream
    image.file = image_directory + img
    # Add the image into the paragraphs collection of the page

This code snippet loops through each JPG file, adds a new page to the PDF document, and inserts the image into the page.

3. Save the Final PDF Document

Finally, after adding all the images, we save the PDF document. The method writes the document to a specified file.

# Save the document"merged-jpgs-to-PDF.pdf")

Combine JPG to PDF - Complete Code Example

Here’s the complete code for combining JPG images into a PDF:

Get a Free License!

Visit our Temporary License page to obtain a free, unrestricted license and fully experience the capabilities of Aspose.PDF for Python without any limitations!

Merge JPG to PDF Online

Additionally, you can merge your JPG images into a PDF document online for free using this JPG to PDF merger web app.

Combine JPG to PDF – Free Resources

Besides combining JPG images into a PDF document, learn how to create, manipulate, and convert PDF documents. Explore various other features of Aspose.PDF for Python using the resources provided below:


In this article, we explored how to combine JPG images into a PDF document using Aspose.PDF for Python. Aspose.PDF library provides a powerful API that allows for extensive customization and control over the resulting PDF document. By following the steps outlined in this guide, you should be able to add multiple JPG images to a single PDF document efficiently and customize the output to suit your needs.

Whether you’re creating photo albums, compiling documents for presentations, or archiving images, Aspose.PDF for Python offers the tools you need to get the job done effectively. If you have any questions, feel free to let us know via our free support forum.

See Also