Created
May 19, 2016 15:29
-
-
Save connorgr/ed0a434385a28da2894755f2fa6bd87f to your computer and use it in GitHub Desktop.
Convert an XLSX file to a LaTeX table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A simple script for creating LaTeX tables from XLSX files. The script assumes | |
that the LaTeX document includes booktabs and tabularx packages. | |
To install pyexcel_xlsx, run ``pip install pyexcel-xlsx``. | |
https://github.com/pyexcel/pyexcel-xlsx | |
''' | |
import argparse | |
from pyexcel_xlsx import get_data | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-f', '--file', type=str, required=True, | |
help="XLSX filename to compile to LaTeX") | |
parser.add_argument('-t', '--tab', type=int, required=False, default=2, | |
help="Number of spaces to use for table tabs") | |
args = parser.parse_args() | |
rows = get_data(args.file) | |
ltxHead = rows[0] | |
rows = rows[1:] | |
t = ''.join([' ' for i in xrange(args.tab)]) | |
print "\\begin{tabularx}{\\linewidth}{" +''.join(["l" for c in ltxHead])+ "}" | |
print t+ "\\toprule" | |
print t + ' & '.join(ltxHead) + " \\\\" | |
print t + "\\midrule" | |
for row in rows: | |
print t + ' & '.join(['' if r is None else r for r in row]) + " \\\\" | |
print t + "\\bottomrule" | |
print "\\end{tabularx}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment