I have an "Exception in Tkinter callback" that seems to arise from within a package I've imported, and not from my own code. How to solve it? Wanting to put data from tkinter into PDF format for printing, I'm running fpdf2 in Python 3.11.3 on Windows 11 Home 23H2. The code line in question is one I copied straight from the documentation at https://py-pdf.github.io/fpdf2/Tables.html.
with pdf.table() as table:
Here's the full traceback on that line.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "C:\Users\Paul\Dropbox\Work\ProgrammingProjects\cellar\main.py", line 1453, in cellar_to_pdf
with pdf.table() as table:
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 144, in __exit__
next(self.gen)
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 5005, in table
table.render()
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\table.py", line 215, in render
row_info = list(self._process_rowpans_entries())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\table.py", line 590, in _process_rowpans_entries
_, img_height, text_height = self._render_table_cell(
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\table.py", line 496, in _render_table_cell
page_break_text, cell_height = self._fpdf.multi_cell(
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 226, in wrapper
return fn(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\deprecation.py", line 32, in wrapper
return fn(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 3652, in multi_cell
text = self.normalize_text(text)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Paul\AppData\Local\Programs\Python\Python311\Lib\site-packages\fpdf\fpdf.py", line 4361, in normalize_text
return text.encode(self.core_fonts_encoding).decode("latin-1")
^^^^^^^^^^^
AttributeError: 'numpy.int64' object has no attribute 'encode'
I'm unable to see where I went wrong. Some help interpreting the traceback would be appreciated.
I got no relevant results by googling "AttributeError:'numpy.int64' object has no attribute 'encode'".
What follows is a minimal, reproducible code example.
from fpdf import FPDF
table_data = (
('Producer', 'Series/Line', 'Wine Name', 'Grape(s)', 'Year', 'Age', 'On Hand'),
('IT CN/331', '', 'Barolo Legato', 'Nebbiolo', '2015', '8y, 11m, 16d', 1, '1 left of 1 stocked'),
('John Galt', '', 'Cabernet Sauvignon', 'Cabernet Sauvignon', '2016', '7y, 11m, 16d', 1, '1 left of 1 stocked'),
('Santos Lima', '', 'Confidencial Reserva', 'Red Blend', '2017', '6y, 11m, 16d', 3, '3 left of 4 stocked'),
)
pdf = FPDF(unit="in", format="Letter")
pdf.add_page()
pdf.set_margins(left=0.75, top=0.75, right=0.75)
pdf.set_font("helvetica", size=10)
with pdf.table() as table:
for tupl in table_data:
row = table.row()
for value in tupl:
row.cell(value)
pdf.output("C:\\Users\\Paul\\Dropbox\\Work\\ProgrammingProjects\\cellar\\x.pdf")
In Jupyter Notebook, this produces a slightly different error.
AttributeError: 'int' object has no attribute 'encode'
For reference, this question was also posted in the fpdf2
GitHub repository:
https://github.com/py-pdf/fpdf2/issues/1270
The reason for this error is simply that you are passing numbers as values to
Row.cell()
. A solution is to convert your values to strings before passing them toRow.cell()
:row.cell(str(value))