python-printer-escpos icon indicating copy to clipboard operation
python-printer-escpos copied to clipboard

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

Open zm08 opened this issue 5 years ago • 2 comments

from escpos.connections import getNetworkPrinter

printer = getNetworkPrinter()(host='***.***.*.*', port=9100)

printer.text("Hello World")
printer.lf()
printer.cut()

(I've blanked out my IP address) produces the following error

Traceback (most recent call last):
  File "E:\Users\Zunyou\Desktop\僔佑\Programming\Tests\test6.py", line 3, in <module>
    printer = getNetworkPrinter()(host='192.168.2.1', port=9100)
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\connections.py", line 306, in __init__
    self.initialize()
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\commandset\generic.py", line 463, in initialize
    self._write(self.__class__.__ESC + '@')
  File "E:\Program Files\Python\Python36\lib\site-packages\escpos\connections.py", line 328, in _write
    self._device.send(msg)
TypeError: a bytes-like object is required, not 'str'

I have made the necessary amendments from the NetworkPrinter instance has no attribute '_Generic__write' issue, and was subsequently hit with this TypeError

zm08 avatar Feb 27 '20 12:02 zm08

I have the same issue and please kindly tell me if you have solved this issue.

zhaowei699 avatar Jul 19 '21 09:07 zhaowei699

Hi, I was stuck with the same issue but I created a fix to it 👍 Now my printer works in my network: Solution is : Edit the following file -> python-printer-escpos/src/main/python/escpos/connections.py at the line 324 add

if(type(msg) == str) :
   msg = str.encode(msg)

Final result should look like :

def _write(self, msg):
    """
    Print any command sent in raw format
    """
    if(type(msg) == str) :
       msg = str.encode(msg)
    
    self._device.send(msg)

Lion3000 avatar Apr 23 '22 11:04 Lion3000