Raja Jain
Raja Jain

Reputation: 23

How can I create a custom PDF directly from Jupyter?

I’m working on a project at my company where I conduct routine experiments as part of production and quality control (QC). We use custom-built instruments that acquire data from a given experiment and store that data on a database on a company server. Our current interface for analyzing this data is to run a Jupyter Notebook, largely based on Python code, that generates various tables and plots with matplotlib based on the data from a given experiment. We currently heavily rely on manually creating reports of this data, such as exporting some of the results from the Jupyter Notebook as a CSV, and importing it to Excel, etc. The ultimate goal at the end of each experiment is to generate a nicely formatted multi-page PDF with all of the essential information and results.

We really need a better and more automated way to generate reports directly from a Jupyter Notebook in a PDF format, so we can easily attach the report to other PDFs such as batch/production records.

I need to report all of the equipment that I use for an experiment as well as the data from that experiment in a clear and concise way. I also need to include a summary of how the experiment was conducted with each experiment. Ideally, I would be able to input the equipment ID#s directly into Jupyter, create a dataframe with all of the experimental data (images, arrays, etc.), and export this information into a PDF template.

The PDF as imagined, will have a few pages/sections which remain essentially the same over consecutive experiments, but will have sections that must be updated for each new dataset and equipment used. I would also like the PDF to be of commercial quality, in that it has my company's name and logo, a header, and footer.

We’ve dabbled a bit with PyFPDF, but it seems that it might be a bit too limited for our needs. We’ve looked at some other tools, but there’s a lot of options and its not obvious what would work best for our goals and would be the easiest to code. Does anyone have any suggestions that could point us in the right direction? Image of one of the pages from the desired PDF output

Upvotes: 2

Views: 1874

Answers (1)

argentum2f
argentum2f

Reputation: 5350

I would also like the PDF to be of commercial quality, in that it has my company's name and logo, a header, and footer.

I'm sure you could do a lot directly with python libraries, but if you really want high quality output, just use an appropriate tool such as latex - especially if you happen to be already familiar with it. If not, it's not hard to learn. I sometimes use python to generate latex output, and then process the results. You can of course call pdflatex from a python script as well.

I'd recommend writing the latex document separately first for one set of data, though, since it's easier to tweak things. Then write a script to generate and process that file for the different sets of data.

See https://tex.stackexchange.com/questions/885/how-can-i-use-latex-from-python for more suggestions on using latex with python.

Here's an example that generates figures, a latex file, and then calls pdflatex to process it. Run this, and the output will be in report.pdf

import numpy as np
import matplotlib.pyplot as plt
import subprocess

# Generate plots
x = np.arange(0,5,.1)
for i in [2,3,4] :
    plt.plot(x,x**i)
    plt.savefig('Fig{}.png'.format(i))

# Start latex document
f = open('report.tex', 'w')
f.write("""
\\documentclass[]{report}
\\nonstopmode
\\usepackage{lipsum}
\\usepackage{graphicx}
\\begin{document}
\lipsum[1-3]
""")

# Make latex figure
f.write("\\begin{figure}[h]\n")
f.write("    \centering\n")
for i in [2,3,4] :
    f.write("    \\includegraphics[width=.3\\textwidth]{{{}}}\n".format('Fig{}.png'.format(i)))

f.write("    \\caption{Polynomials of power 2,3,4}")
f.write("\end{figure}\n")

# End latex document
f.write("\\end{document}\n")
f.close()

# Compile latex document
subprocess.call('ls')
subprocess.call(['pdflatex', 'report.tex'])

Upvotes: 1

Related Questions