Skip to content

Instantly share code, notes, and snippets.

@irth
Created December 15, 2019 18:28
Show Gist options
  • Save irth/d55f91abd2475c3793e22e71009f6e8b to your computer and use it in GitHub Desktop.
Save irth/d55f91abd2475c3793e22e71009f6e8b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
CLIPBOARD_IN_CMD=['xclip', '-o', '-sel', 'CLIPBOARD']
CLIPBOARD_OUT_CMD=['xclip', '-i', '-sel', 'CLIPBOARD']
import subprocess
p = subprocess.run(CLIPBOARD_IN_CMD, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
lines = [
l.strip().split("\t")
for l in p
.stdout
.decode('utf-8')
.replace("%", "\%")
.strip()
.split("\n")
]
header, rest = lines[0], lines[1:]
def row_to_latex(row, wrap_math=False):
return " &\t".join([("$%s$" % cell) if wrap_math else cell for cell in row]) + " \\\\\n";
converted="""\\begin{table}[H]
\\centering
\\begin{tabular}{""" + "|".join(["c"]*len(header)) + "}\n "
converted += row_to_latex(header);
converted += " \hline\n"
for row in rest:
converted += " "+ row_to_latex(row, wrap_math=True)
converted += """ \end{tabular}
\end{table}"""
subprocess.run(CLIPBOARD_OUT_CMD, input=converted.encode('utf-8'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment