Home > other >  Convert parts of .ods table to latex with Pandas/Python
Convert parts of .ods table to latex with Pandas/Python

Time:08-07

i'm trying to convert a selection of rows and columns from an libreoffice-spreadsheet to Latex-code using Python with Pandas in a Jupyter-Notebook. The goal is to write it in a Python-script, but because, right now, i'm not used to working with Python i chose Jupyter-Notebook as beginner-friendly test-platform.

Unfortunately, the output is not what i want. The Pandas-doc is a little bit confusing, because there seem to be variants of converting i don't really understand. Right now, this is what i got so far:

import pandas as pd

original_chronologie = pd.read_excel('Florian-Chronotology-Greco-Roman.ods', engine='odf', nrows=34)

ptolemies_select = original_chronologie.iloc[15:22]

ptolemies_select.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID", "names", "types", "description", "timeOriginal"])

/tmp/ipykernel_2308/3169131508.py:1: FutureWarning: In future versions DataFrame.to_latex is expected to utilise the base implementation of Styler.to_latex for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use DataFrame.style.to_latex which also contains additional functionality. ptolemies_select.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID", "names", "types", "description", "timeOriginal"])

ptolemies_select.style.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID", "names", "types", "description", "timeOriginal"])

TypeError Traceback (most recent call last) Input In [8], in <cell line: 1>() ----> 1 ptolemies_select.style.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID", "names", "types", "description", "timeOriginal"])

TypeError: to_latex() got an unexpected keyword argument 'na_rep'

ptolemies_select.style.to_latex('aegyptische_chrono.tex', clines="all;data")

From the first warning output i tried the style.to_latex option, but didn't worked out---like the error-message shows.

The Pandas-doc only lists Latex-special formatting parameters for the style.to_latex function, but no possibilities to select rows, columns or entry-fields. The Type Error sentence suggests that the parameter from the regular to_latex function are not allowed inside the style.to_latex version.

The output of the first command ptolemies_select.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID" actually looks good for me...

\begin{tabular}{llllll}
    \toprule
    {} &      importID &                                              names &      types &                                        description &   timeOriginal \\
    \midrule
    15 &  spp2143:0364 &  Ptolemy VI Philometor and Cleopatra I@enPtolem... &  political &  An uneasy peace was made with Syria in 192 whe... &     180-176 BC \\
    16 &  spp2143:0365 &  Ptolemy VI Philometor and Cleopatra II@enPtoel... &  political &  When his mother died in 176 BC Ptolemy VI was ... &     176-170 BC \\
    17 &  spp2143:0366 &  Ptolemy VI Philometor, Ptolemy VIII Euergetes ... &  political &  Lenaios and Eulaios, the two guardians of the ... &     170-163 BC \\
    18 &  spp2143:0367 &  Ptolemy VI Philometor and Cleopatra II 2nd rei... &  political &  Shortly after Ptolemy VI was expulsed by his y... &     163-145 BC \\
    19 &  spp2143:0368 &  Ptolemy VII Neos Philopator@enPtolemaios VII N... &  political &  The widowed Cleopatra was left in Alexandria w... &     145BC (1)  \\
    20 &  spp2143:0369 &  Ptolemy VIII Euergetes II and Cleopatra II@enP... &  political &  Upon his brother’s death in 145 BC, Ptolemy VI... &  145-141/40 BC \\
    21 &  spp2143:0370 &  Ptolemy VIII Euergetes II, Cleopatra II and Cl... &  political &  Around 140 BC, Ptolemy VIII married his niece,... &  141/40-116 BC \\
    \bottomrule
    \end{tabular}

with the big exception that fields with much text, like description, are cut off after a few words. I couldn't find out, how to change this, and therefore tried the style function with no success.

I know, with enough time i'll figure it out by myself. But, unfortunately, i'm in a hurry, because the project has to be ready until end of August. So, if someone familiar with Python and Pandas knows an easy and fast solution, i will be very thankful.

Best

CodePudding user response:

You need to modify an option of the Pandas library in order to show the full data in the column. You need to add this snippet at the beginning of your code (after the import statement) :

pd.set_option('display.max_colwidth', None)

Extract from Pandas 1.4.3 documentation :

display.max_colwidth : int or None

The maximum width in characters of a column in the repr of a pandas data structure. When the column overflows, a “…” placeholder is embedded in the output. A ‘None’ value means unlimited. [default: 50]

  • Related