How To Convert Multiple Images Into A Single PDF File Using 4 Lines of Python Code
Tags: python, codenewbie, tutorial, programming
Published: 2023-07-18 18:51:57

Today, you will create a project that will grab all of the image files from a particular directory and create a single PDF file including all of the images. 

The interesting thing about Python is, you will need **only 4 lines of code** to achieve that! So, let's get started, shall we?

## 💡 Detailed Article

I have written the complete article on [freeCodeCamp](https://www.freecodecamp.org/news/convert-multiple-images-into-a-single-pdf-file-with-python/). Make sure to read that too!

{% embed https://www.freecodecamp.org/news/convert-multiple-images-into-a-single-pdf-file-with-python/ %}

## 📺 Video Guideline

I know that many of you like to watch video instead of following a complete article. Fear not! I have also created a complete video tutorial just for you!

{% embed https://youtu.be/zBZhfzgahsk %}

## 🎯 Pre Requisite

1. Simply create a new folder for the project. Please make sure that you do not keep any spaces in the folder's name.
2. Keep some image files in that directory. For this project, I am going to use the `.jpg` image files. Therefore I would suggest you to do the same thing!
3. You can download the royalty free images from [Unsplash](https://unsplash.com/) or [Pexels](https://unsplash.com/). Keep in mind that our project can't handle large image files. Therefore, try to download those image files which are smaller in file size. You can select small files during downloading them from Unsplash.
4. Now simply open the Visual Studio Code. If you want, then obviously you can use other text editors or IDEs. But for simplicity, I like to use widely used free software. And VS Code is the best in this sector.
5. Create a Python file where you will write down the entire code with me. I am going to create a file named `Script.py`.
6. Install the package/library named `img2pdf`. This library is used for converting images to PDF via direct JPEG inclusion. You can check this [website](https://pypi.org/project/img2pdf/) for more details. I am going to install it using pip. Simply open a terminal and use the command, `pip install img2pdf`.

## 💻 Code

```
import os
import img2pdf
with open("ImageContainingBook.pdf", "wb") as file:
    file.write(img2pdf.convert([i for i in os.listdir("C:\\Users\\fahim\\Desktop\\ImageToPdf") if i.endswith(".jpg")])) # Change the file directory accordingly
```

⭐ The complete project is available on [GitHub](https://github.com/FahimFBA/img2singlePDF).

Here, I imported two necessary packages first, `os` and `img2pdf`. Then I need to specify exactly in which file format and file name I want to place my image files. As I want to generate a PDF file to include all of my image files, I have specified that.

Then I need to specify what I want to do with the file. I want to write in that file and I want the conversion functionality of the `img2pdf` library. In my specified directory, there might be a lot of different files and that is natural. But as I only want to convert the image files which have a `.jpeg` extension in them, I need to specify that explicitly. Also, I definitely need to include the file directory where it will get all of the images.

## 🏃‍♂️ Run

If you have the Code Runner extension installed on your VS Code, then you can simply run the file using that extension.


As my filename is Script.py and I am using my Windows machine, my command would be `python Script.py`. If you are a Linux or Mac user, then use the command `python3 Script.py` or `python3 filename.py`.

## 💁‍♂️ Conclusion


I hope you have enjoyed this short article.  😊

If you have any questions then please let me know using [Twitter](https://twitter.com/Fahim_FBA) or [LinkedIn](https://www.linkedin.com/in/fahimfba/).

You can also follow me on:
🎁GitHub: [FahimFBA](https://github.com/FahimFBA)
🎁YouTube: [@FahimAmin](https://www.youtube.com/@FahimAmin?sub_confirmation=1)

If you are interested then you can also check my website: [https://fahimbinamin.com/](https://fahimbinamin.com/)

Have a great day! 😊

🖼️ Cover: Photo by Christina Morillo: https://www.pexels.com/photo/woman-programming-on-a-notebook-1181359/