Skip to content

Commit

Permalink
Add phd utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann21 committed Sep 1, 2023
1 parent 71fd2e7 commit fa5b1e0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions yann_utils/phd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pandas as pd
import subprocess
import os

def csv_to_latex_png(df, png_filename):
# 1. Convert the df to LaTeX table format
latex_code = r"""
\documentclass{standalone}
\usepackage{booktabs}
\begin{document}
"""
latex_code += df.to_latex(index=False, escape=False)
latex_code += r"\end{document}"

with open("temp_table.tex", "w") as latex_file:
latex_file.write(latex_code)

# 2. Compile the LaTeX to produce a PDF
subprocess.call(["pdflatex", "-interaction=nonstopmode", "temp_table.tex"])

# 3. Convert the PDF to PNG
subprocess.call(["pdftoppm", "-png", "temp_table.pdf", "temp_table"])

# Rename to the desired png filename
os.rename("temp_table-1.png", png_filename)

# Clean up intermediate files
for ext in [".tex", ".aux", ".log", ".pdf"]:
os.remove("temp_table" + ext)

0 comments on commit fa5b1e0

Please sign in to comment.