Convert Email to PDF in Python using PDF Extractor Web API
In this detailed tutorial, we will guide you through the process of converting an email file (either .eml or .msg) to a PDF document using the PDF.co Web API in Python. We will use a sample .eml file for this demonstration, but you can apply the same steps to .msg files as well.

Step 1: Install the requests Library
Before starting, you need to install the requests library. This Python library is essential for making HTTP requests to the PDF.co Web API, which will allow you to interact with PDF.co’s services.
- Open your terminal or command line interface (CLI).
- Run the following command to install the
requestsmodule:python -m pip install requests
This will install the requests library in your Python environment, enabling you to communicate with the PDF.co Web API.
Step 2: Access the Source Code
Next, you need to prepare the Python script that will handle the conversion of the email file into a PDF.
- Copy the sample Python code from the link provided (or use the code example below).
- Paste the code into your preferred Python code editor, such as Visual Studio Code, PyCharm, or any other Python-compatible editor.
Step 3: Configure the Python Code
Now that you have the sample code, let’s configure it with your specific settings.
API Key:
- Obtain your API Key from your PDF.co dashboard.
- Add your API key to the designated spot in the Python script.
Source File URL:
- Provide the URL for the email file you want to convert into a PDF. If the email file is stored locally on your machine, you need to upload it to PDF.co’s File Storage.
- Once uploaded, you will receive a file URL (token URL) that you can use in the script to reference the file for conversion.
Output PDF Name:
- Choose the desired name for the output PDF file where the converted content will be saved. Modify this in the code as needed.
Asynchronous Mode:
- For efficiency, we recommend using Asynchronous Mode during the conversion process. This mode allows the conversion to run in the background, enabling the program to continue executing without waiting for the conversion to complete.
Paper Size:
- Specify your desired paper size (e.g., Letter, A4) for the generated PDF output. Adjust the
paperSizeparameter in the code to match your requirements.
Embed Attachments
- Set
embedAttachmentstoTruein Python if the output should include the email’s attachments. Set it toFalseto convert only the email body.
Convert Attachments:
- With attachments included, set
convertAttachmentstoTrueto convert supported attachment types into PDF pages and merge them into the output. Unsupported formats remain embedded files. - Set
convertAttachmentstoFalseto retain the original files as embedded attachments instead. See the current supported-format list. - Inspect both the PDF pages and its attachment list. Use a PDF reader with attachment support, since a browser preview may not show embedded files.

Step 4: Save Python Program
Once you have updated the code with your API Key, email file URL, and parameter settings, save the Python program in your preferred directory.

Step 5: Run the Program
Now, execute the program. If everything is configured correctly, the program will start the email-to-PDF conversion. Once the conversion is complete, you will receive a generated PDF URL.
- You can copy and paste the URL into your browser to view the generated PDF.
- Alternatively, navigate to the directory where the Python script is located, and you will find the converted PDF file saved with the specified name.

Step 6: View PDF Result
After the script finishes running and the PDF file has been generated, open the output PDF using your preferred PDF viewer. You should now see the content of your email file converted into a neatly formatted PDF document.

You’ve learned how to convert email to PDF in Python using PDF Extractor API with this tutorial. This is just one of the examples of how PDF.co Web API can make things easier for Python developers.
Related Tutorials
