In this tutorial, we’re going to show you how to extract images from PDF in Python and convert them back to PDF.

We prepare this sample PDF that we will use for this tutorial.

Source PDF
Source PDF

Here’s what the output would look like.

Output PNG for Page 1
Output PNG for Page 1
Output PNG for Page 2
Output PNG for Page 2

How to Extract Images from PDF in Python

Follow these steps to extract images from a PDF file in Python:

  1. Install request module
  2. Add API Key
  3. Path to Source and Output File
  4. Setup Parameters
  5. Run Program
  6. Extract Image Demo

Step 1 – Install request module

  • Have installed the request module already? If not, kindly install the requests module. You can do it by running this python -m pip install requests in your command line.

Step 2 – Add API Key

  • Insert your API key into your Python code. You may get your API key here.

PDF.co API Key

Step 3 – URL to Source and Path to Output File

  • Add the URL to your source and path for your output file

Add URL for your source file

Path for Output File

Step 4 – Setup Parameters

  • Set up the parameters for your PDF to PNG conversion

Setup parameters

Step 5 – Run Program

  • Once you’re done with your code, you may now run your program. You can see the output file in your folder

Run PDF to PNG program

Step 6 – Extract Image Demo

  • Here’s the program in action
PDF to PNG Demo using Python
PDF to PNG Demo using Python

How to Convert Them Back into PDF

Follow these steps to convert images back to PDF in Python.

  1. Path to Source and Output File
  2. Setup Parameters
  3. Run Program
  4. PNG to PDF Demo

Step 1 – URL to Source and Path to Output

  • Add the URL to your source and path for your output file

Add URL of your source and path for your output

Step 2 – Setup Parameters

  • Set up the parameters for your PNG to PDF conversion

Setup parameters for PNG to PDF

Step 3 – Run Program

  • Once you’re done with your code, you may now run your program. You can see the output file in your folder

Run PNG to PDF program

Step 4 – PNG to PDF Demo

  • Here’s what the program in action looks like
PNG to PDF Demo using Python
PNG to PDF Demo using Python

You’ve learned how to convert PDF to PNG and convert the PNG back to PDF in Python with PDF Extractor API.

Related Pages:

Related Samples: