将 excel 表格转换为 latex 表格

2020-04-15 15:05发布

复杂表格的编写在`latex`中一直是一件很麻烦的事情,体验过几个表格转换工具,结果都不尽人意,要么表格格式不对,要么生成的代码难以理解,对后续调整十分不利。 于是我用`python`编写了一个脚本...

复杂表格的编写在`latex`中一直是一件很麻烦的事情,体验过几个表格转换工具,结果都不尽人意,要么表格格式不对,要么生成的代码难以理解,对后续调整十分不利。 于是我用`python`编写了一个脚本工具,将 excel 转换为 latex 表格,它具有以下优点: - 实现了一套对复杂表格的处理流程 - 生成的代码易于理解和维护,方便后续进行个性化调整 项目地址:[https://github.com/ZhiyuanLck/excel2tex](https://github.com/ZhiyuanLck/excel2tex) 下面展示两个复杂表格的转换例子 示例1 ![](https://pics.latexstudio.net/data/images/202004/00dd6d13f0cdf03.png) 转换命令: ```shell python excel2tex.py -s test.xlsx -o table.tex ``` `main.tex` ```tex \documentclass{article} \usepackage{xeCJK} \usepackage{multirow, makecell} \begin{document} \input{table.tex} \end{document} ``` 生成的`table.tex`文件: ```tex % Please add the following required packages to your document preamble: % \usepackage{multirow, makecell} \begin{tabular}{*{5}{|c}|} \hline % row 1 plain单元格 & \multicolumn{2}{c|}{multicolumn测试} & \multicolumn{2}{c|}{block测试} \\ \hline % row 2 \multicolumn{2}{|c|}{不是} & \multirowcell{2}{multirow \\测试} & 6 & 7 \\ \cline{1-2} \cline{4-5} % row 3 1 & 2 & & 8 & 9 \\ \hline % row 4 3 & 4 & 5 & \multirowcell{2}{A} & \multirowcell{2}{B} \\ \cline{1-3} % row 5 \multicolumn{3}{|c|}{总结} & & \\ \hline \end{tabular} ``` 编译结果: ![](https://pics.latexstudio.net/data/images/202004/479cebe2553aeb0.png) 示例2 ![![](https://pics.latexstudio.net/data/images/202004/213db4421f0c0b1.png)](https://pics.latexstudio.net/data/images/202004/001bff9cf7a28d2.png) 转换命令: ```shell python excel2tex.py -s table.xlsx --math ``` 生成的代码: ```tex % Please add the following required packages to your document preamble: % \usepackage{multirow, makecell} \begin{tabular}{*{7}{|c}|} \hline % row 1 \multirowcell{6}{10} & \multicolumn{6}{c|}{11} \\ \cline{2-7} % row 2 & \multirowcell{4}{6} & \multicolumn{4}{c|}{7} & \multirowcell{6}{12} \\ \cline{3-6} % row 3 & & \multirowcell{2}{2} & \multicolumn{2}{c|}{3} & \multirowcell{4}{8} & \\ \cline{4-5} % row 4 & & & $x + y = z$ & \multirowcell{2}{4} & & \\ \cline{3-4} % row 5 & & \multicolumn{2}{c|}{5} & & & \\ \cline{2-5} % row 6 & \multicolumn{4}{c|}{9} & & \\ \cline{1-6} % row 7 \multicolumn{6}{|c|}{13} & \\ \hline \end{tabular} ``` 编译结果: ![](https://pics.latexstudio.net/data/images/202004/213db4421f0c0b1.png)