How to Add Tables to PDF Files with Python
Why Add Tables to PDF with Python?
The pdfRest Add to PDF API Tool is a powerful resource for developers looking to manipulate PDF documents programmatically. This tutorial will guide you through the process of making an API call to the Add to PDF endpoint using Python. By leveraging this tool, you can automate the addition of tables and other elements to your PDFs, enhancing their utility and presentation.
Imagine you're managing a project and need to regularly update stakeholders with the latest project status in a PDF format. Using the Add to PDF API, you can programmatically insert a table into a PDF document that outlines project milestones, owners, and statuses. This can save significant time and ensure that your documents are consistently formatted and up-to-date.
Add Tables to PDF with Python Code Example
import json
import requests
from requests_toolbelt import MultipartEncoder
# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
# api_url = "https://eu-api.pdfrest.com"
# Add an accessible project-status table with a header, colored status cells, and a footer.
table_objects = [
{
"page": 1,
"x": 54,
"y": 540,
"width": 504,
"columns": [{"width": 210}, {"width": 144}, {"width": 150}],
"tag_structure_type": "Table",
"style": {
"padding": {"top": 6, "right": 8, "bottom": 6, "left": 8},
"text_size": 10,
},
"header_rows": [
{
"cells": [
{"text": "Milestone", "tag_structure_type": "TH", "style": {"background_color_rgb": [26, 72, 112], "text_color_rgb": [255, 255, 255]}},
{"text": "Owner", "tag_structure_type": "TH", "style": {"background_color_rgb": [26, 72, 112], "text_color_rgb": [255, 255, 255]}},
{"text": "Status", "tag_structure_type": "TH", "style": {"background_color_rgb": [26, 72, 112], "text_color_rgb": [255, 255, 255]}},
]
}
],
"rows": [
{"cells": [{"text": "Requirements review"}, {"text": "Maya Chen"}, {"text": "Complete", "tag_structure_type": "TD", "style": {"background_color_rgb": [220, 252, 231]}}]},
{"cells": [{"text": "Prototype delivery"}, {"text": "Jordan Lee"}, {"text": "In progress", "tag_structure_type": "TD", "style": {"background_color_rgb": [254, 249, 195]}}]},
{"cells": [{"text": "Stakeholder approval"}, {"text": "Avery Patel"}, {"text": "Planned", "tag_structure_type": "TD", "style": {"background_color_rgb": [239, 246, 255]}}]},
],
"footer_rows": [
{"cells": [{"text": "Next review: Friday, 10:00 AM", "col_span": 3, "tag_structure_type": "TD", "style": {"background_color_rgb": [245, 247, 250], "text_color_rgb": [55, 65, 81]}}]}
],
}
]
with open("/path/to/input.pdf", "rb") as input_file:
multipart = MultipartEncoder(
fields={
"file": ("input.pdf", input_file, "application/pdf"),
"table_objects": json.dumps(table_objects),
"tag_enabled": "true",
"tag_language": "en-US",
"output": "project-status",
}
)
response = requests.post(
f"{api_url}/pdf-with-added-tables",
data=multipart,
headers={
"Accept": "application/json",
"Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # Replace with your API key.
"Content-Type": multipart.content_type,
},
)
print(f"Response status code: {response.status_code}")
print(json.dumps(response.json(), indent=2) if response.ok else response.text)
Source: GitHub Repository
Breaking Down the Code
The code begins by importing necessary libraries, including json for handling JSON data and requests along with MultipartEncoder from requests_toolbelt for making multipart HTTP requests.
api_url = "https://api.pdfrest.com"
This line sets the base URL for the API. The default is the US-based service, but there's an option for a GDPR-compliant EU-based service.
table_objects = [
{
"page": 1,
"x": 54,
"y": 540,
"width": 504,
"columns": [{"width": 210}, {"width": 144}, {"width": 150}],
"tag_structure_type": "Table",
"style": {
"padding": {"top": 6, "right": 8, "bottom": 6, "left": 8},
"text_size": 10,
},
...
}
]
This dictionary defines the table to be added to the PDF. It specifies the page number, coordinates, dimensions, and style of the table. The tag_structure_type is set to "Table", indicating the structure of the content.
with open("/path/to/input.pdf", "rb") as input_file:
multipart = MultipartEncoder(
fields={
"file": ("input.pdf", input_file, "application/pdf"),
"table_objects": json.dumps(table_objects),
"tag_enabled": "true",
"tag_language": "en-US",
"output": "project-status",
}
)
This section opens the input PDF file and prepares the multipart form data. The fields dictionary includes the file, table objects, tagging options, and output file name. The json.dumps() function converts the table_objects dictionary into a JSON string.
response = requests.post(
f"{api_url}/pdf-with-added-tables",
data=multipart,
headers={
"Accept": "application/json",
"Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", # Replace with your API key.
"Content-Type": multipart.content_type,
},
)
This block sends the POST request to the API endpoint, including necessary headers such as the API key and content type. The response from the API is captured in the response variable.
Beyond the Tutorial
In this tutorial, you learned how to use Python to make a multipart API call to the pdfRest Add to PDF endpoint, adding a table to a PDF document. This example demonstrates the flexibility of the pdfRest API in automating PDF manipulations.
To explore further, try out all the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide. This example is a multipart API call. For examples using JSON payloads, visit the GitHub Repository.