2

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.

1 Answer 1

1

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()))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.