Reputation: 526
I found quite a nice way to export a table generated with pandas
here to PDF, the part about converting it to a png file is uninteresting to me.
The problem is, that I get the following error message:
---------------------------------------------------------------------------
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'
I'm not really understanding the code in the first place which makes it quite hard to correct the error. My code looks like this:
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])
where z
is a DataFrame
generated with pandas
.
Hope someone can help out. Thank you in advance, Sito.
Upvotes: 2
Views: 7553
Reputation: 4821
The problem is that you're opening a file to write in bytes mode - that's what the "b" character means in the call to open()
- and then passing it string data. Change this:
with open(filename, 'wb') as f:
f.write(template.format(z.to_latex()))
to this:
with open(filename, 'w') as f:
f.write(template.format(z.to_latex()))
Upvotes: 1