【问题标题】:Exporting pandas table as pdf将熊猫表导出为 pdf
【发布时间】:2017-10-13 08:46:52
【问题描述】:

我发现了一种将pandas here 生成的表格导出为 PDF 的好方法,关于将其转换为 png 文件的部分对我来说并不感兴趣。

问题是,我收到以下错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-9818a71c26bb> in <module>()
     13 
     14 with open(filename, 'wb') as f:
---> 15     f.write(template.format(z.to_latex()))
     16 
     17 subprocess.call(['pdflatex', filename])

TypeError: a bytes-like object is required, not 'str'

我一开始并没有真正理解代码,这使得纠正错误非常困难。我的代码如下所示:

import subprocess

filename = 'out.tex'
pdffile = 'out.pdf'

template = r'''\documentclass[preview]{{standalone}}
\usepackage{{booktabs}}
\begin{{document}}
{}
\end{{document}}
'''

with open(filename, 'wb') as f:
    f.write(template.format(z.to_latex()))

subprocess.call(['pdflatex', filename])

其中z 是使用pandas 生成的DataFrame

希望有人能帮忙。 先感谢您, 西托。

【问题讨论】:

    标签: python pandas pdf


    【解决方案1】:

    问题是您正在打开一个文件以字节模式写入——这就是“b”字符在调用open() 中的含义——然后将字符串数据传递给它。改变这个:

    with open(filename, 'wb') as f:
        f.write(template.format(z.to_latex()))
    

    到这里:

    with open(filename, 'w') as f:
        f.write(template.format(z.to_latex()))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 2020-11-07
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多