我想为特定流程生成一份报告,其中包含多个页面上的长数据框。但当我尝试生成它时,表格只出现在一页上,而不是整个页面。我尝试了该程序包,longtable
但它不起作用。
这是我当前的代码(已更新):
import pandas as pd
import pylatex as pl
df = pd.DataFrame(dict(x=range(256),y=['same']*256))
doc = pl.Document()
doc.packages.append(pl.Package('booktabs'))
doc.packages.append(pl.Package('longtable'))
with doc.create(pl.Section('Table: Global Faults ')):
with doc.create(pl.Table(position='htbp')) as table:
table.add_caption('Five first')
table.append(pl.Command('centering'))
table.append(pl.NoEscape(df.to_latex(longtable=True)))
doc.generate_pdf(filepath=f'Global_Fault', clean_tex=False)
答案1
环境longtable
不应在环境内部使用table
。标题应作为参数提供df.to_latex
。居中是默认设置。
梅威瑟:
import pandas as pd
import pylatex as pl
df = pd.DataFrame(dict(x=range(256),y=['same']*256))
doc = pl.Document()
doc.packages.append(pl.Package('booktabs'))
doc.packages.append(pl.Package('longtable'))
with doc.create(pl.Section('Table: Global Faults ')):
doc.append(pl.NoEscape(df.to_latex(longtable=True,caption='Five first')))
doc.generate_pdf(filepath=f'Global_Fault', clean_tex=False)
生成的 LaTeX:
\begin{document}%
\normalsize%
\section{Table: Global Faults }%
\label{sec:TableGlobalFaults}%
\begin{longtable}{lrl}
\caption{Five first}\\
\toprule
{} & x & y \\
\midrule
\endhead
\midrule
\multicolumn{3}{r}{{Continued on next page}} \\
\midrule
\endfoot
\bottomrule
\endlastfoot
0 & 0 & same \\
1 & 1 & same \\
% etc.
结果:
[...]