Learn how to Combine JPG Images to PDF in Python
Created
July 6, 2024 09:57
Combine JPG Images to PDF in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Directory containing JPG images | |
image_directory = "D:\\Files\\images\\" | |
# Get a list of all JPG files in the directory | |
file_paths = sorted([os.path.join(image_directory, f) for f in os.listdir(image_directory) if f.startswith("sam") and f.endswith(".jpg")]) | |
import os | |
import aspose.pdf as pdf | |
from aspose.pdf import Document, Image, Page | |
# Create a PDF document and add the image to it | |
doc = Document() | |
page = doc.pages.add() | |
# Add images to the page | |
for img in file_paths: | |
# Create an image object | |
image = Image() | |
# Set the image file stream | |
image.file = img | |
# Add the image into the paragraphs collection of the page | |
page.paragraphs.add(image) | |
# Save the document | |
doc.save("merged-jpgs-to-PDF.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment