avatarAjay Parmar

Summary

The web content provides an overview of nine Python libraries that facilitate the transformation of raw data into ready-to-publish reports, offering solutions for table extraction, data visualization, PDF editing, and HTML/CSS to PDF conversion.

Abstract

The article introduces Python developers to a curated list of libraries designed to streamline the process of creating polished reports from raw data. It begins with Camelot for extracting tables from PDFs and Sweetviz for generating visual reports with minimal setup. The list continues with Tabulate for creating styled tables, PyPDF2 for PDF editing tasks, Yattag for HTML/XML syntax creation, PrettyTable for console-based tabular reports, MPLD3 combined with Matplotlib for interactive data visualization, WeasyPrint for converting HTML/CSS layouts to PDFs, and PyFPDF2 for generating customizable modern PDFs. Each library is accompanied by examples and code snippets to illustrate its capabilities, emphasizing ease of use and the ability to produce publication-ready outputs without extensive coding.

Opinions

  • The author end Ajay, expresses a preference for using Yattag for designing websites, noting its simplicity in modifying Python code without the need to write HTML syntax manually.
  • Sweetviz is highlighted for its simplicity and ease of publishing reports quickly, although it is acknowledged that it offers limited customization options compared to other tools like Metabase or Plotly.
  • The author suggestsends PyPDF2 for its versatility in handling various PDF editing tasks, such as splitting, merging, compressing, and adding watermarks, under tight deadlines.
  • MPLD3 is described as an enhancer for Matplotlib plots, adding interactivity and improving the presentation of data visualizations with minimal code changes.
  • WeasyPrint is praised for its ability to preserve the original format of HTML and CSS layouts when converting them into PDFs, maintaining elements like headers, logos, footers, and contact details.
  • PyFPDF2 is noted for its popularity in business settings, particularly for its extensive customization options that allow for the addition of text, images, and tables in PDFs.
  • The author encourages reader engagement by inviting them to clap, comment, follow on Medium and LinkedIn, join an email list, and follow the "Top Python Libraries" publication for more content.

9 Python Libraries That Turn Raw Data into Ready-to-Publish Reports

Image generated by author using Ideogram.

#1. How to Extract Tables from PDFs Using the Python Library Camelot

Image by the Author

In this ready-to-publish report, first, we have Camelot. Camelot is an excellent PDF library that can extract tables from PDFs based on the folder file path you provide in your code. It automatically extracts data from the PDF and consolidates it in one place.

By adjusting just a few lines of code, as shown in the image, you can see how the PDF file, which I’ve taken a screenshot of, displays the extracted table based on the file path provided from my computer.

You can easily find the file location by clicking the folder search bar. Once you’ve located it, paste the path into a code editor. For example, look at the PDF file below.

/path/to/your/pdf/financial_report.pdf
import camelot

# Specify the PDF file path
file_path = ""/path/to/your/pdf/financial_report.pdf""

# Read the PDF and extract tables
tables = camelot.read_pdf(file_path, pages="1")  # Specify the page number(s)

# Print the number of tables found
print(f"Total tables found: {len(tables)}")

# Display the extracted data from the first table
print(tables[0].df)

# Export the first table to a CSV file
output_csv = "quarterly_revenue.csv"
tables[0].to_csv(output_csv)

print(f"Table saved to {output_csv}")

Now, once you click the “Run” button, it will take a few moments to generate the data, which you can easily copy and paste for your next project.

How simple is that? You can use VS Code or Jupyter Notebook, which are both simple options, or you can choose any other editor you prefer.

Console Output: Total tables found: 1

Image by the Author

#2. Sweetviz: The Best Library for Ready-to-Publish Reports

GIF as the output of this program

Another library you can use for a ready-to-publish report is Sweetviz. It’s a report visualization library that works with pandas for raw data processing. First, it takes files like CSS, processes them, and generates a report in a web format.

Unlike Metabase, which requires a database, or libraries like Plotly, which involve high-level interactions and coding, Sweetviz focuses on keeping things simple with minimal setup. This allows users to quickly publish their reports, but that’s why it has limited customization options.

# Install Sweetviz
!pip install sweetviz

# Import necessary libraries
import pandas as pd
import sweetviz as sv

# Your example data
data = {
    'Customer ID': ['0001', '0002', '0003', '0004', '0005', '0006'],
    'Age': [25, 32, 24, 30, 28, 25],
    'Location': ['Delhi', 'Mumbai', 'Bangalore', 'Pune', 'Delhi', 'Bangalore'],
    'Search Query': ['Casual Summer Wear', 'Formal Office Wear', 'Eco-friendly Clothing', 'Party Dresses', 'Casual Summer Wear', 'Sustainable Fashion'],
    'Product Preference': ['Summer Dress', 'Blazers', 'Organic Cotton Dress', 'Evening Gown', 'Casual T-shirt', 'Eco-friendly Jeans']
}

# Convert the dictionary to a DataFrame
df = pd.DataFrame(data)

# Create a Sweetviz report
report = sv.analyze(df)

# Show the report in the notebook and save it as an HTML file
report.show_html('sweetviz_report.html')

# Download the Sweetviz report
from google.colab import files
files.download('sweetviz_report.html')

Output: —

Screenshot by the author of the output

#3. Tabulate: Easily Convert Data into Styled Tables

Tabulate offers extensive features for tables in report outputs, enhancing the user experience. It works with borders, formatting, columns, sorting, text wrapping, and more.

Not only does it look great, but it also makes the data ready for publication with just a few lines of code. There’s no need for users to create tables manually in Excel sheets or elsewhere.

from tabulate import tabulate

# Data
data = {
    'Customer ID': ['0001', '0002', '0003', '0004', '0005', '0006'],
    'Age': [25, 32, 24, 30, 28, 25],
    'Location': ['Delhi', 'Mumbai', 'Bangalore', 'Pune', 'Delhi', 'Bangalore'],
    'Search Query': ['Casual Summer Wear', 'Formal Office Wear', 'Eco-friendly Clothing', 'Party Dresses', 'Casual Summer Wear', 'Sustainable Fashion'],
    'Product Preference': ['Summer Dress', 'Blazers', 'Organic Cotton Dress', 'Evening Gown', 'Casual T-shirt', 'Eco-friendly Jeans']
}

# Generate the table in Asciidoc Mono format
table = tabulate(data, headers="keys", tablefmt="asciidoc_mono")

# Print the table
print(table)

Output: —

Customer ID    Age  Location    Search Query           Product Preference
-------------  -----  ----------  ---------------------  --------------------
         0001     25  Delhi       Casual Summer Wear     Summer Dress
         0002     32  Mumbai      Formal Office Wear     Blazers
         0003     24  Bangalore   Eco-friendly Clothing  Organic Cotton Dress
         0004     30  Pune        Party Dresses          Evening Gown
         0005     28  Delhi       Casual Summer Wear     Casual T-shirt
         0006     25  Bangalore   Sustainable Fashion    Eco-friendly Jeans

#4. Edit PDFs Using PyPDF2

In a ready-to-publish report, if time is short and the workload is heavy, use PyPDF2. It’s a super Python library that offers editing options for your PDF data.

With PyPDF2, you can split PDFs, merge them, compress files, add watermarks, and decrypt PDFs — everything you need, all with this powerful library.

how to slpit pdf

from PyPDF2 import PdfWriter, PdfReader

reader = PdfReader("example.pdf")
writer = PdfWriter()

for page in reader.pages[0:2]:  # Extract first two pages
    writer.add_page(page)

with open("split.pdf", "wb") as output_pdf:
    writer.write(output_pdf)

How to merge pdf

from PyPDF2 import PdfMerger

merger = PdfMerger()
merger.append("file1.pdf")
merger.append("file2.pdf")
merger.write("merged.pdf")
merger.close()

#5. Yattag: Simplifying Report Creation with Clean HTML/XML Syntax

In a ready-to-publish report, Yattag plays a critical role in creating HTML syntax. Just modify your Python code according to the desired report or HTML structure.

Personally, I would use it for designing my website, and this can be done by making changes in just a few lines of code. The interesting part is that I don’t need to write any HTML syntax — I just need to modify the Python code with the Yattag library.

from yattag import Doc

doc, tag, text = Doc().tagtext()

with tag('html'):
    with tag('body'):
        with tag('h1'):
            text('Hello, Yattag!')

print(doc.getvalue())

Adding CSS/JavaScript

with tag('style'):
    text('body { font-family: Arial; }')
with tag('script'):
    text('console.log("Hello from Yattag!");')

#6. PrettyTable: For Ready-to-Publish Console Reports

PrettyTable is a tabular library that takes input in tabular form, as you can see below, and transforms it into a table. It also comes with multiple features that allow you to edit the table, all by making just a few changes in the code.

from prettytable import PrettyTable

# Example data
data = {
    'Customer ID': ['0001', '0002', '0003', '0004', '0005', '0006'],
    'Age': [25, 32, 24, 30, 28, 25],
    'Location': ['Delhi', 'Mumbai', 'Bangalore', 'Pune', 'Delhi', 'Bangalore'],
    'Search Query': ['Casual Summer Wear', 'Formal Office Wear', 'Eco-friendly Clothing', 'Party Dresses', 'Casual Summer Wear', 'Sustainable Fashion'],
    'Product Preference': ['Summer Dress', 'Blazers', 'Organic Cotton Dress', 'Evening Gown', 'Casual T-shirt', 'Eco-friendly Jeans']
}

# Create a PrettyTable object
table = PrettyTable()

# Setting field names
table.field_names = ["Customer ID", "Age", "Location", "Search Query", "Product Preference"]

# Adding rows to the table
for i in range(len(data['Customer ID'])):
    table.add_row([
        data['Customer ID'][i],
        data['Age'][i],
        data['Location'][i],
        data['Search Query'][i],
        data['Product Preference'][i]
    ])

# Print the table
print(table)

Output: —

+--------------+-----+-----------+-----------------------+------------------------+
| Customer ID  | Age | Location  |      Search Query      |   Product Preference    |
+--------------+-----+-----------+-----------------------+------------------------+
|     0001     |  25 |   Delhi   | Casual Summer Wear     |      Summer Dress       |
|     0002     |  32 |  Mumbai   | Formal Office Wear     |        Blazers          |
|     0003     |  24 | Bangalore | Eco-friendly Clothing  |  Organic Cotton Dress   |
|     0004     |  30 |   Pune    | Party Dresses          |     Evening Gown        |
|     0005     |  28 |   Delhi   | Casual Summer Wear     |     Casual T-shirt      |
|     0006     |  25 | Bangalore | Sustainable Fashion    |  Eco-friendly Jeans     |
+--------------+-----+-----------+-----------------------+------------------------+

#7. Are MPLD3 and Matplotlib a Powerful Combination for Data Visualization?

MPLD3 is like the “decoration on a well-prepared dish,” where the dish is Matplotlib and the decoration is MPLD3.

In the ready-to-publish concept, MPLD3 makes the report more interactive by adding features with just a few changes in the code.

# Install MPLD3
!pip install mpld3

# Import necessary libraries
import matplotlib.pyplot as plt
import mpld3
from mpld3 import plugins

# Your example data
data = {
    'Customer ID': ['0001', '0002', '0003', '0004', '0005', '0006'],
    'Age': [25, 32, 24, 30, 28, 25],
    'Location': ['Delhi', 'Mumbai', 'Bangalore', 'Pune', 'Delhi', 'Bangalore'],
    'Search Query': ['Casual Summer Wear', 'Formal Office Wear', 'Eco-friendly Clothing', 'Party Dresses', 'Casual Summer Wear', 'Sustainable Fashion'],
    'Product Preference': ['Summer Dress', 'Blazers', 'Organic Cotton Dress', 'Evening Gown', 'Casual T-shirt', 'Eco-friendly Jeans']
}

# Creating a scatter plot
fig, ax = plt.subplots()
scatter = ax.scatter(data['Age'], data['Location'], color='b', s=100, alpha=0.5)

# Adding tooltips with customer information
tooltip_labels = [
    f"ID: {data['Customer ID'][i]}<br>Search: {data['Search Query'][i]}<br>Product: {data['Product Preference'][i]}"
    for i in range(len(data['Customer ID']))
]
tooltip = plugins.PointLabelTooltip(scatter, labels=tooltip_labels)
plugins.connect(fig, tooltip)

# Set plot title and labels
ax.set_title('Customer Age vs. Location with Interactive Tooltips', size=16)
ax.set_xlabel('Age')
ax.set_ylabel('Location')

# Save the plot to an HTML file
html_code = mpld3.fig_to_html(fig)
with open('interactive_plot.html', 'w') as f:
    f.write(html_code)

# Download the HTML file
from google.colab import files
files.download('interactive_plot.html')
Image graph generated by the author using Matplotlib

#8. Can WeasyPrint Abstract PDFs from HTML and CSS Layouts?

Syntra’s website, which we are using to extract data from

In this project, we will be using WeasyPrint. WeasyPrint is a Python library that allows us to create a PDF directly from HTML and CSS. The image below shows an abstracted HTML and CSS file from the Syntra site, which also includes a GIF from a popular clothing brand.

For the project, we will use the WeasyPrint library to transform this into a ready-to-publish PDF. One thing I personally like about WeasyPrint is that it preserves elements like the header, logo, footer, and contact details, making the PDF look exactly like the original format — almost as if you took a screenshot, but it generates a PDF instead.

Image is the raw data of HTML and CSS

Install WeasyPrint pip

pip install weasyprint
from weasyprint import HTML

# Path to your HTML file
html_file_path = 'example.html'

# Path to your output PDF file
pdf_output_path = 'output_report.pdf'

# Convert the HTML (and CSS if linked in the HTML) to a PDF
HTML(html_file_path).write_pdf(pdf_output_path)

# Print the output file path
print(f"PDF has been generated and saved to: {pdf_output_path}")

#9. Can You Generate Modern PDFs Using Just a Few Lines of PyFPDF2 Code?

Turn your code into an easy-to-publish PDF with just a few simple Python steps. Not only that, but it also allows you to customize your PDF programmatically with a simple Python library called PyFPDF2. PyFPDF2 is a popular library in the business field for ready-to-publish documents, especially known for its customization options.

First, choose your code editor — I’ve chosen Google Colab here, as I felt it’s a great fit for this project. Then, simply install the PyFPDF2 library using the command below:

from google.colab import files
uploaded = files.upload()
Screen shot by the Author

Below, I have generated a PDF based on the information I fed into the program and executed in the code editor. I simply used Google Colab as the code editor, and boom, it generated the ready-to-publish document.

One thing I noticed is that the customization options allow you to add text, images, tables, and more. Finally, when I executed the command, the PDF was automatically downloaded to my laptop.

pdf.output(r"C:\Your\Desired\Path\financial_report.pdf")
Screen shot by the Author

Hey there, I’m Ajay — a passionate engineer, writer on Medium, and a huge Python enthusiast. Thanks for sticking with me till the end! If you enjoyed this content and want to support my work, the best ways are:

Thanks again for reading! 😊

Python Libraries
Python
Report
Python Programming
Python Packages
Recommended from ReadMedium