我一直在尝试使用this accepted answer as a reference复制熊猫DF 到 TeX 到 PDF 到 png配方。但是,我的Python 3(使用Jupyter Notebook通过Anaconda Navigator)给我带来了一些麻烦。
我已经在本地磁盘上的以下默认位置安装了MiKTeX:
C:\Users\USERNAME\AppData\Local\Programs\MiKTeX\2.9
我还在以下位置安装了LaTex:
C:\Users\USERNAME\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\notebook\static\components\MathJax\extensions\TeX
但是,当我使用Pandas df对象运行代码块时:
filename = 'out.tex'
pdffile = 'out.pdf'
outname = 'out.png'
template = r'''\documentclass[preview]{{standalone}}
\usepackage{{booktabs}}
\begin{{document}}
{}
\end{{document}}
'''
with open(filename, 'wb') as f:
f.write(bytes(template.format(df.to_latex()),'UTF-8'))
subprocess.call(['pdflatex', filename], shell=True)
subprocess.call(['convert', '-density', '300', pdffile, '-quality', '90', outname])
它只是返回“ 4”,而从我的原始df创建的唯一文件是“ out.tex ”。我希望将DF转换为png图像,以便将整个过程保留在Notebook脚本中。
任何建议将不胜感激。谢谢。
答案 0 :(得分:0)
我通过以下两个流程调用解决了这个问题:
subprocess.call('latex -interaction=batchmode -output-directory='+ outpath + ' ' + input_tex_file, shell=True, stdout=open(os.devnull, 'wb'))
subprocess.call('dvipng -q* -T tight -o ' + output_png_file + ' ' + input_dvi_file, shell=True, stdout=open(os.devnull, 'wb'))
您必须首先根据原始的乳胶数据创建.tex
文件。然后,这些命令首先从.dvi
文件创建一个.tex
文件(和一些其他辅助文件)。然后,您可以使用dvipng(https://ctan.org/pkg/dvipng?lang=en)创建一个.png
文件。