Latex and Python integration

Asked

Viewed 4,262 times

2

I need to generate and save a PDF file with some data I treated in a script I did to generate report, however, I do not know how to do it.

Do these data need to go through a . tex file before generating the PDF? If yes, how can I work with variables(?) within the . tex file? For example, modify the header, add data in columns (data from a dataframe), add images containing graphs.

If not, how to generate a PDF file with Python?

OBS.: I already have a file . tex that was written and configured by a third party, I just need to integrate this file with python.

  • Have you considered using the pweave? I use a similar tool on R, calling for knitr, and have achieved very good results.

1 answer

6


Reportlab:

With reportlab Voce you can generate pdfs without the need to generate one .tex, but if you know latex and the task you are doing is academic and/or scientific, I would opt for Pylatex.

Pylatex:

"Python library for creating and compiling files (or fragments) latex. The goal of this package is to provide an extensive and easy-to-use interface between python and latex."

Basic example (from the documentation):

from pylatex import Document, Section, Subsection, Command
from pylatex.utils import italic, NoEscape


def fill_document(doc):
    """Add a section, a subsection and some text to the document.

    :param doc: the document
    :type doc: :class:`pylatex.document.Document` instance
    """
    with doc.create(Section('A section')):
        doc.append('Some regular text and some ')
        doc.append(italic('italic text. '))

        with doc.create(Subsection('A subsection')):
            doc.append('Also some crazy characters: $&#{}')


if __name__ == '__main__':
    # Basic document
    doc = Document('basic')
    fill_document(doc)

    doc.generate_pdf(clean_tex=False)
    doc.generate_tex()

    # Document with `\maketitle` command activated
    doc = Document()

    doc.preamble.append(Command('title', 'Awesome Title'))
    doc.preamble.append(Command('author', 'Anonymous author'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

    fill_document(doc)

    doc.generate_pdf('basic_maketitle', clean_tex=False)

    # Add stuff to the document
    with doc.create(Section('A second section')):
        doc.append('Some text.')

    doc.generate_pdf('basic_maketitle2', clean_tex=False)
    tex = doc.dumps()  # The document as string in LaTeX syntax

Generated file:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
%
\title{Awesome Title}%
\author{Anonymous author}%
\date{\today}%
%
\begin{document}%
\normalsize%
\maketitle%
\section{A section}%
Some regular text and some %
\textit{italic text. }%
\subsection{A subsection}%
Also some crazy characters: \$\&\#\{\}

%
\section{A second section}%
Some text.

%
\end{document}

Compiled Tex (basic_maketitle2.pdf):

PDF gerado

Browser other questions tagged

You are not signed in. Login or sign up in order to post.