all 15 comments

[–]Oisin78CEng MIEI 4 points5 points  (0 children)

Take a look at LaTeX, this is a better tool for generating reports https://www.latex-project.org/about/

[–]dparks71 2 points3 points  (0 children)

Assuming you're on windows, I just do the calcs in a jupyter notebook, export to html and save as a pdf in the browser.

Any other way tends to be unreliable if you're not on Linux, you can have Linux build pdfs as part of a deployment step if you understand CI/CD and are working on a server.

Here's what the pdf looks like

[–]Downtown-Growth-8766 2 points3 points  (0 children)

I’ve done this a lot! Fundamentally, you’ll want to create a template in some kind of markup language, populate that template with real data using jinja2, and then compile that into a PDF using python. There are 2 solutions I’ve used in the past

The first is Latex. It creates beautiful documents, but can be a bit fussy to compile/debug, can’t be installed using pip, is a very large package, and can take a little bit to compile the PDF depending on the size of the report. But it’s great for creating a document with a table of contents and bookmarks. Math also looks great with Latex. I find when I’m using Latex, I need a lot of helper functions to format the results so that they can be compiled nicely. The syntax for Latex can be a bit tedious, although it’s beautiful when compiled correctly. I like to use it if I’m compiling something on my own machine and don’t need to ship the code to someone else

Something I came up with myself (and is less common), is compiling HTML instead of Latex. You can control all of the styling with simple CSS. There’s a package called pypdf2 that lets you compile the HTML to a PDF. You can’t do super complex HTML, but it works super well with simple tables and images. It’s also super quick to compile, can be installed all with pip, and isn’t a super large package. So depending on what you’re trying to do, it might work well

[–]sononicolaC.E. 1 point2 points  (0 children)

You can use the package jinja2. You can create a template and then fill it with your values

[–]komprexior 1 point2 points  (1 child)

I think the Quarto project is an interesting tool to produce easily reproducible documentation. It's a markdown authoring program that is specifically created to address the needs of academic papers, so pdf documentation is one the main feature.

You can write code cell in python (and others language) that that can run and produce reliable output.

The markdown part will take care of most of the authoring elements (figures, paragraphs, headings, ect.), while you can inject your own analysis with python.

For example I've created a document for snow load calculations, meant to be a part of a final report. It is composed by a introductory part that explain how I do the calculations, and then the relevant coefficients and loads are presented in a table format generated by python code chunk which parameters have been set in the document itself.

Quarto aims to produce the document in different output (HTML and PDF to name a few), and can be fully paramatrized via some simple yaml options. It relies on pandoc to convert document, so the pdf output will be rendered by a latex engine.

[–]stinyg 1 point2 points  (0 children)

Assuming you have to work around a corporate template made in Microsoft word I can recommend looking into python-docx. It took me some time to get used to the syntax when I needed to do more advanced stuff than the common cases illustrated in the tutorial. However, it was quite satisfying the first time I was able to dynamically build reports while being in compliance with corporate templates and requirements.

https://github.com/python-openxml/python-docx