有什么方法可以向to_latex()生成的表添加边框吗?

时间:2019-03-18 07:50:30

标签: pandas latex pylatex

我正在通过将DataFrame传递到to_latex()来创建表,该表将生成无边界的表。有什么办法添加吗?

2 个答案:

答案 0 :(得分:0)

import re # Use regular expression to search for column declaration section of LaTeX table
tex_content = df.to_latex(index=True, escape=False) #Or whatever options you want/need
print(tex_content)
re_borders = re.compile(r"begin\{tabular\}\{([^\}]+)\}")
borders = re_borders.findall(tex_content)[0]
borders = '|'.join(list(borders))
tex_content = re_borders.sub("begin{tabular}{" + borders + "}", tex_content)
print(tex_content)
# Or save tex_content to a ".tex" file

答案 1 :(得分:-1)

请参阅https://www.overleaf.com/learn/latex/Tables

\begin{center}

\begin{tabular}{ |c|c|c| } 

 \hline

 cell1 & cell2 & cell3 \\ 

 cell4 & cell5 & cell6 \\ 

 cell7 & cell8 & cell9 \\ 

 \hline

\end{tabular}

\end{center}