繁体   English   中英

使用 Pandas/Python 将部分 of.ods 表转换为 latex

[英]Convert parts of .ods table to latex with Pandas/Python

我正在尝试使用 Python 和 Jupyter-Notebook 中的 Pandas 将选择的行和列从 libreoffice 电子表格转换为乳胶代码。 目标是用 Python 脚本编写它,但是因为现在我不习惯使用 Python 我选择 Jupyter-Notebook 作为初学者友好的测试平台。

不幸的是,output 不是我想要的。 Pandas-doc 有点令人困惑,因为似乎有一些我不太了解的转换变体。 现在,这是我到目前为止得到的:

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:在未来的版本中, DataFrame.to_latex预计将利用Styler.to_latex的基本实现进行格式化和渲染。 因此,arguments 签名可能会发生变化。 建议改用DataFrame.style.to_latex ,它还包含其他功能。 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() 得到了一个意外的关键字参数“na_rep”

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

从第一个警告 output 开始,我尝试了style.to_latex选项,但没有成功——就像错误消息显示的那样。

Pandas-doc 仅列出style.to_latex function 的 Latex 特殊格式参数,但没有 select 行、列或条目字段的可能性。 类型错误语句表明来自常规to_latex function 的参数在style.to_latex版本中是不允许的。

第一个命令ptolemies_select.to_latex('aegyptische_chrono.tex', na_rep="--", columns=["importID"实际上对我来说看起来不错...

\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}

一个很大的例外是包含大量文本的字段,例如description ,在几句话之后就被切断了。 我不知道如何更改它,因此尝试了 function style但没有成功。

我知道,有足够的时间我会自己解决的。 但是,不幸的是,我很着急,因为这个项目要到八月底才能准备好。 因此,如果熟悉 Python 和 Pandas 的人知道一个简单快捷的解决方案,我将非常感激。

最好的

您需要修改 Pandas 库的选项,以便在列中显示完整数据。 您需要在代码开头添加此代码段(在 import 语句之后):

pd.set_option('display.max_colwidth', -1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM