HTML JPG PDF XML DOCX
  Product Family
PDF

Replace an Image in PDF via Python SDK

Replace Images in a PDF Document using Cloud Python SDK

Get Started

How to replace Images in PDF via Python SDK

To replace images in PDF via Cloud Python SDK, we’ll use Aspose.PDF Cloud Python SDK. This Cloud SDK assists Python programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Python programming language via Aspose.PDF REST API. If the python package is hosted on Github, you can install directly from Github:

Installation from Github

     
    pip install git+https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-python.git

Package Manager Console Command

     
    pip install asposepdfcloud

Steps to replace images in PDF using Python

Aspose.PDF Cloud developers can easily load & replace PDF images in just a few lines of code.

  1. Initialize the Aspose PDF API
  2. Upload the PDF Document to Cloud Storage
  3. Reads a local image file and uploads it to the Aspose.PDF Cloud storage.
  4. Inserts the uploaded image into the uploaded PDF document at a specific location using image Id.
  5. Download the result if needed it
 

Replace images in PDF using Python


    import shutil
    import json
    import logging
    from pathlib import Path
    from asposepdfcloud import ApiClient, PdfApi, Color, Bookmark

    # Configure logging
    logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")

    class ImagesReplace:
        """Class for add image to PDF using Aspose PDF Cloud API."""
        def replaceImageInPdf(self):
            """Append a new image to the PDF document."""
            localFolder = "C:\Samples"
            storageDocumentName = "sample.pdf"
            localImagePath = "C:\\Samples\\sample.jpg"
            storageImageFile = "sample.jpg"
            storageTempFolder = "TempPdfCloud"
            outputFileName = "output_replace_images.pdf"
            imageId = 'GE5TCMJQGUZDWMJZGMWDOMZSFQZDSOBMG42DK'

            # Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
            self.pdf_api = PdfApi(ApiClient(AppSecret, AppSid))

            if self.pdf_api:
                file_path = localFolder + '/' + storageDocumentName
                self.pdf_api.upload_file(storageTempFolder + '/' + storageDocumentName, file_path)

                self.pdf_api.upload_file(storageTempFolder + "/" + storageImageFile, localImagePath)

                opts = {
                    "image_file_path" : storageTempFolder + "/" + storageImageFile,
                    "folder" : storageTempFolder
                }
                self.pdf_api.put_replace_image(storageDocumentName, imageId, **opts)

                temp_file = self.pdf_api.download_file(storageTempFolder + '/' + storageDocumentName)
                local_path = localFolder + '/' + outputFileName
                shutil.move(temp_file, str(local_path))
                logging.info(f"download_result(): File successfully downloaded: {local_path}")
 

Work with Images in PDF

Images make documents visually engaging and help break up large blocks of text, making them more attractive to readers. Photos, screenshots, or icons can add context to the content, making it more relatable and meaningful for the audience. Including images in PDFs is essential for creating engaging presentations, user manuals, marketing brochures, and educational materials. Add the Images into PDF documents with Aspose.PDF Cloud Python SDK.

With our Python library you can:

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to change image dimensions in PDF files online and test the functionality.