Create portable documents from Python

By Nobody 2025-11-01

Python

Coding

Introduction

In order to create documents that can be shared without installing commercial or proprietary software, where said documents may include graphics and text, there are a few interesting options.

PDF

Python Steps

python -m venv py_env
source py_env/bin/activate.
pip install --upgrade pip
pip install fpdf

An example to do this:

from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="This is some sample text.", ln=True, align="C")
pdf.cell(200, 10, txt="Another line of text.", ln=True, align="L")

# Add a PNG image
# Replace 'your_image.png' with the actual path to your PNG file
# x, y are the coordinates for the top-left corner of the image
# w, h are the width and height of the image in the PDF
pdf.image('bolt_cloud.png', x=10, y=40, w=100)
pdf.output("output_with_image_and_text.pdf")

Having done all that, turns out there is a newer version:

fpdf2

And going from markdown or HTML to PDF might be nice:

fpdf2 markdown.

Or try using matplotlib with it:

fpdf2 matplotlib

ODS

Might have the advantages of worksheet format, e.g. the data can be opened in any program and operated upon, whereas PDF is nice report but not as nice for later analysis.

from collections import OrderedDict
from pyexcel_ods import save_data

# Prepare your data
data = OrderedDict()
data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})
data.update({"Sheet 2": [["Header A", "Header B"], ["Value 1", "Value 2"]]})

# Save the data to an ODS file
save_data("my_output.ods", data)
print("ODS file 'my_output.ods' created successfully.")

However pyexcel-ods might not do images.

dding a PNG image to an ODS file is a more complex task that generally requires a lower-level, more feature-complete library than simple data-oriented ones like pyexcel-ods3 [1]. The odfdo library is suitable for this purpose as it allows for manipulation of the document structure itself