-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
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
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) |