Created
January 18, 2017 20:30
-
-
Save nwertzberger/b8e07e27dbb39bf7e1a2777923e4c094 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# A little script that helps convert csv files into a latex longtable appendix file. | |
import sys | |
import csv | |
# Title, Letter, Column_count, Title, Title, Letter, Columns, Col Count, Columns, Col count, footer, Col count, footer | |
longtable_header = """%% !TEX root = paper_that_includes_this_appendix.tex | |
\\chapter{%s} | |
\\label{app:appendix%s} | |
\\begin{landscape} | |
\\begin{longtable}{%s} | |
\\caption[%s]{%s} | |
\\label{t:table%s} \\\\ | |
\\hline | |
%s \\\\ | |
\\hline | |
\\endfirsthead | |
\\multicolumn{%d}{c}{{\\bfseries \\tablename\\ \\thetable{} -- continued from previous page}} \\\\ | |
\\hline | |
%s \\\\ | |
\\hline | |
\\endhead | |
\\hline | |
\\multicolumn{%d}{|r|}{{ | |
%s | |
}} \\\\ | |
\\hline | |
\\endfoot | |
\\hline | |
\\multicolumn{%d}{|r|}{{ | |
%s | |
}} \\\\ | |
\\hline | |
\\endlastfoot | |
""" | |
longtable_footer = """ | |
\\end{longtable} | |
\\end{landscape} | |
""" | |
def gen_longtable_header(title, letter, header_columns, footer): | |
column_types = "|" | |
longtable_header_columns = "" | |
for col in header_columns: | |
column_types += "l|" | |
longtable_header_columns += col + " & " | |
longtable_header_columns = longtable_header_columns[:-2] | |
return longtable_header % ( | |
title, | |
letter, | |
column_types, | |
title, | |
title, | |
letter, | |
longtable_header_columns, | |
len(header_columns), | |
longtable_header_columns, | |
len(header_columns), | |
footer, | |
len(header_columns), | |
footer) | |
def gen_longtable_footer(): | |
return longtable_footer | |
def gen_longtable(table_name, appendix_letter, csv_name, footer_text): | |
with open(csv_name, 'r') as csvfile: | |
stats = csv.reader(csvfile) | |
header = next(stats) | |
print(gen_longtable_header(table_name, appendix_letter, header, footer_text)) | |
for row in stats: | |
print " & ".join(row) + " \\\\" | |
print(gen_longtable_footer()) | |
if __name__ == "__main__": | |
gen_longtable(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment