如果你有pdflatex和ImageMagick的安裝,你可以導出數據幀到特克斯,使用pdflatex將其轉換爲PDF文件,然後使用ImageMagick的PDF轉換成PNG:
import pandas as pd
import numpy as np
import subprocess
df = pd.DataFrame({'d': [1., 1., 1., 2., 2., 2.],
'c': np.tile(['a', 'b', 'c'], 2),
'v': np.arange(1., 7.)})
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(template.format(df.to_latex()))
subprocess.call(['pdflatex', filename])
subprocess.call(['convert', '-density', '300', pdffile, '-quality', '90', outname])
如果安裝phantomjs和ImageMagick的,你可以 導出數據框爲HTML,然後使用phantomjs到HTML轉換成PNG和ImageMagick的裁剪結果:
import pandas as pd
import numpy as np
import subprocess
df = pd.DataFrame({'d': [1., 1., 1., 2., 2., 2.],
'c': np.tile(['a', 'b', 'c'], 2),
'v': np.arange(1., 7.)})
filename = '/tmp/out.html'
outname = '/tmp/out.png'
cropname = '/tmp/cropped.png'
with open(filename, 'wb') as f:
f.write(df.to_html())
rasterize = '/path/to/phantomjs/examples/rasterize.js'
subprocess.call(['phantomjs', rasterize, filename, outname])
subprocess.call(['convert', outname, '-trim', cropname])
你爲什麼不只是包括從Excel表格中的PowerPoint演示文稿,而無需創建任何圖像? – 2014-10-31 16:12:25
我會盡量避免使用Excel,即python => Powerpoint – user2370852 2014-10-31 18:29:38
我不知道你確切的先決條件是什麼,但也許你應該重新考慮這一點。將數據渲染爲圖像並不是最好的想法,因爲之後不能更改外觀和大小,從中複製,字體變爲柵格化,並且需要更多空間。 – 2014-10-31 19:28:37