Python Forum
print a persian file by thermal printer and python-escpos mnodule
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print a persian file by thermal printer and python-escpos mnodule
#1
hello friends...
i want to print a persian file by thermal printer..
i can print an english file esily..but i face the proble while printing persian text..
i think i don't do some principles...i have studied about unicode encodeand decode in python..but it seems it isn't enough 
please guide me

my code is in python3 :
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Print an Arabic string to a printer.
# Based on example from escpos-php

# Dependencies-
# - pip install wand python-bidi python-escpos
# - sudo apt-get install fonts-hosny-thabit
# - download arabic_reshaper and place in arabic_reshaper/ subfolder

import arabic_reshaper
#from escpos import printer
from escpos.printer import Usb
from bidi.algorithm import get_display
from wand.image import Image as wImage
from wand.drawing import Drawing as wDrawing
from wand.color import Color as wColor

p = Usb(0x04b8,0x0e15,0)

p.codepage="cp720"   #设置解码的类型

# Some variables
#fontPath = "/usr/share/fonts/opentype/fonts-hosny-thabit/Thabit.ttf"
textUtf8 = u"بعض النصوص من جوجل ترجمة"
tmpImage = 'my-text.png'
printWidth = 550

# Get the characters in order
textReshaped = arabic_reshaper.reshape(textUtf8)
textDisplay = get_display(textReshaped)

# PIL can't do this correctly, need to use 'wand'.
# Based on
# https://stackoverflow.com/questions/5732408/printing-bidi-text-to-an-image
im = wImage(width=printWidth, height=36, background=wColor('#ffffff'))
draw = wDrawing()
draw.text_alignment = 'right';
draw.text_antialias = False
draw.text_encoding = 'utf-8'
draw.text_kerning = 0.0
draw.font_size = 36
draw.text(printWidth, 22, textDisplay)
draw(im)
im.save(filename=tmpImage)

# Print an image with your printer library
p.set(align="right")
p.image(tmpImage)
p.cut()
in the above code, i used the different codepages, but my output from the printer is just question mark ""?"" instead of the persian words
i tried the following code,too:
#coding=utf8

from escpos.printer import Usb
""" Seiko Epson Corp. Receipt Printer M129 Definitions (EPSON TM-T88IV) """

p = Usb(0x04b8,0x0e15,0)

p.codepage="iso8859_6"  

# Print text
p.text(u"سلام\n")

p.cut()
but the printer prints the obscure letters...
i tried the different codepages..but it wasn't usefull
Reply
#2
Have you tried using escpos' Magic Encoder to see if it can guess which codepage to use?  https://python-escpos.readthedocs.io/en/...agicEncode
Reply
#3
(Aug-10-2017, 05:19 PM)nilamo Wrote: Have you tried using escpos' Magic Encoder to see if it can guess which codepage to use?  https://python-escpos.readthedocs.io/en/...agicEncode

i haven't seen any example about how i can use this module Undecided Undecided Undecided
Reply
#4
(Aug-11-2017, 05:24 AM)gray Wrote: i haven't seen any example about how i can use this module

hmmm, did you try the Usage Page ?
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#5
My guess would be:
p = Usb(0x04b8,0x0e15,0)

from ecspos.magicencode import MagicEncoder

encoder = MagicEncoder(p)
encoder.write("...something")
Reply
#6
(Aug-11-2017, 02:34 PM)nilamo Wrote: My guess would be:
p = Usb(0x04b8,0x0e15,0)

from ecspos.magicencode import MagicEncoder

encoder = MagicEncoder(p)
encoder.write("...something")

i used the above code,it couldn't know usb..
i used the following code
from escpos import *

p = printer.Usb(0x04b8,0x0e15,0)

encoder = magicencode.MagicEncoder(p)
encoder.write("...something")
i got this error :

Traceback (most recent call last):
  File "/home/pi/start/printerr/thermal-printer/arabicc/magencod.py", line 6, in <module>
    encoder = magicencode.MagicEncoder(p)
NameError: name 'magicencode' is not defined
 shoud i install magicencode module???
how???
Reply
#7
It probably is installed, you just need to import it.  import * might not import sub-modules, and isn't recommended for... any reason.
Reply
#8
(Aug-12-2017, 11:27 PM)nilamo Wrote: It probably is installed, you just need to import it.  import * might not import sub-modules, and isn't recommended for... any reason.
 
i used this code:

#from escpos import *
from escpos.printer import Usb
from ecspos.magicencode import MagicEncoder
p = Usb(0x04b8,0x0e15,0)


encoder = magicencode.MagicEncoder(p)
encoder.write("...something")
but i got this error :

Traceback (most recent call last):
  File "/home/pi/start/printerr/thermal-printer/arabicc/magencod.py", line 4, in <module>
    from ecspos.magicencode import MagicEncoder
ImportError: No module named 'ecspos'
what is wrong?
Reply
#9
It is MagicEncode not MagicEncoder .
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#10
(Aug-13-2017, 11:49 AM)sparkz_alot Wrote: It is MagicEncode not MagicEncoder .

i again got the same error : "No module named 'ecspos'
" Cry Huh  Undecided
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to send file to printer with no results. chob_thomas 2 3,391 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
  Saving the print result in a text file Calli 8 1,810 Sep-25-2022, 06:38 PM
Last Post: snippsat
  How do you marshal the default printer setup to print? hammer 0 1,283 May-29-2022, 02:54 PM
Last Post: hammer
  Can you print a string variable to printer hammer 2 1,960 Apr-30-2022, 11:48 PM
Last Post: hammer
  failing to print not matched lines from second file tester_V 14 6,107 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,226 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 10,992 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,815 Oct-17-2021, 05:08 AM
Last Post: davidm
  Why it does not print(file.read()) Rejaul84 1 2,351 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  get two characters, count and print from a .txt file Pleiades 9 3,388 Oct-05-2020, 09:22 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020