Skip to content

Instantly share code, notes, and snippets.

@connorgr
Created May 19, 2016 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save connorgr/ed0a434385a28da2894755f2fa6bd87f to your computer and use it in GitHub Desktop.
Save connorgr/ed0a434385a28da2894755f2fa6bd87f to your computer and use it in GitHub Desktop.
Convert an XLSX file to a LaTeX table
'''
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