我正在尝试将一些 pandas 数据框表导出为图像。我查看了许多解决方案,并找到了最好的选择。我按照以下说明进行操作这个网站 https://medium.com/@andy.lane/convert-pandas-dataframes-to-images-using-imgkit-5da7e5108d55,并且本地一切正常。
但是,当尝试让此 python 脚本在 EC2 服务器上运行时,出现以下错误:
OSError: wkhtmltoimage exited with non-zero code 1. error:
qt.qpa.screen: QXcbConnection: Could not connect to display
Could not connect to any X display.
You need to install xvfb(sudo apt-get install xvfb, yum install xorg-x11-server-Xvfb, etc), then add option: {"xvfb": ""}.
我尝试按照指示安装到 xfvb,但这不起作用(我想是因为 wkhtmltopdf 是从另一个库调用的)。我启动了一台新服务器,从头开始重新安装所有内容,以消除潜在的问题。依然没有。
我用谷歌搜索了这个问题并尝试了一些随机建议,但没有任何结果。
我的目标是从 pandas 数据帧生成 SVG 文件(转换为 html)。没有显示器的云服务器可以实现吗?有没有更好的方法从 pandas 生成 PDF 报告的表格图像?
一些代码:
import pandas
data = pandas.read_csv(open("biostats.csv", "r"))
css = """
<style type=\"text/css\">
table {
color: #333;
font-family: Helvetica, Arial, sans-serif;
width: 640px;
border-collapse:
collapse;
border-spacing: 0;
}"""
text_file = open("filename.html", "a")
# write the CSS
text_file.write(css)
# write the HTML-ized Pandas DataFrame
text_file.write(data.to_html())
imgkitoptions = {"format": "svg"}
imgkit.from_file("filename.html", outputfile, options=imgkitoptions)
text_file.close()