by this module you can correct your text shape an direction. just install pips and use it.

# install: pip install --upgrade arabic-reshaper
import arabic_reshaper

# install: pip install python-bidi
from bidi.algorithm import get_display

text = "ذهب الطالب الى المدرسة"
reshaped_text = arabic_reshaper.reshape(text)    # correct its shape
bidi_text = get_display(reshaped_text)           # correct its direction
Answer from Jalal Razavi on Stack Overflow
🌐
PyPI
pypi.org › project › PyArabic
PyArabic · PyPI
A specific Arabic language library for Python, provides basic functions to manipulate Arabic letters and text, like detecting Arabic letters, Arabic letters groups and characteristics, remove diacritics etc.
🌐
GitHub
github.com › mpcabd › python-arabic-reshaper
GitHub - mpcabd/python-arabic-reshaper: Reconstruct Arabic sentences to be used in applications that don't support Arabic · GitHub
For this example to work you need to run pip install --upgrade arabic-reshaper python-bidi pillow · import arabic_reshaper text_to_be_reshaped = 'اللغة العربية رائعة' reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped) # At this stage the text is reshaped, all letters are in their correct form # based on their surroundings, but if you are going to print the text in a # left-to-right context, which usually happens in libraries/apps that do not # support Arabic and/or right-to-left text rendering, then you need to use # get_display from python-bidi.
Author: mpcabd
🌐
OneCompiler
onecompiler.com › python › 3vgzuqbwf
Python print - arabic - Python - OneCompiler
import sys name = sys.stdin.readline() print("Hello "+ name) Python is a very popular general-purpose programming language which was created by Guido van Rossum, and released in 1991. It is very popular for web development and you can build almost anything like mobile apps, web apps, tools, data analytics, machine learning etc.
🌐
YouTube
youtube.com › watch
Python in Arabic 53 ِArabic Text كيفية التعامل مع الكتابة بالعربي بالبايثون - YouTube
AboutPressCopyrightContact usCreatorsAdvertiseDevelopersTermsPrivacyPolicy & SafetyHow YouTube worksTest new featuresNFL Sunday Ticket · © 2025 Google LLC
Published: July 22, 2019
🌐
Stack Overflow
stackoverflow.com › questions › 46570002 › printing-arabic-text-file-in-python3
python - Printing Arabic text file in python3 - Stack Overflow
October 4, 2017 - It prints the output correctly in Ipython (pycharm), a little bit garbled in the terminal. I'm using python 3.6.1 ... Save this answer. ... Show activity on this post. ... Copyimport arabic_reshaper text_to_be_reshaped = 'اللغة العربية رائعة' reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped) display_text = get_display(reshaped_text) #### important print(display_text)
🌐
Pub.dev
pub.dev › packages › dartarabic
dartarabic | Dart package
October 19, 2024 - A specific Arabic language library ported to dart from Python, provides basic functions to manipulate Arabic letters and text. ... Import 'package:dartarabic/dartarabic.dart' and access the static methods in DartArabic class, and Arabic class. Get strings of Arabic language characters by accessing Arabic class and Arabic.Symbols. for example: print(Arabic.ALEF); print(Arabic.BEH); print(Arabic.TEH); print(Arabic.Symbols.QUESTION); print(Arabic.Symbols.SEMICOLON); print(Arabic.Symbols.SHADDA);
Published: Mar 15, 2021
Version: 0.3.1
🌐
YouTube
youtube.com › sentdex
Readin Arabic in Python Converting from Unicode to characters and symbols in Python p.1 - YouTube
Part 2: http://youtu.be/nQkaBiOwsIo As requested, this is a tutorial showing users how to handle unicode on websites like Twitter. This can be used to conver
Published: November 14, 2013
Views: 19K
🌐
Stack Overflow
stackoverflow.com › questions › 50338795 › print-arabic-words-and-list-in-python-2-7
unicode - print arabic words and list in python 2.7 - Stack Overflow
May 14, 2018 - Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am using anaconda Python 2.7 for Arabic text classification when I print words or list or words it appears in Unicode I want to print the real Arabic words the list contians [Arabic sentence, label]
Find elsewhere
🌐
Readthedocs
camel-tools.readthedocs.io › en › latest › api › utils › charsets.html
camel_tools.utils.charsets — camel_tools 1.5.2 documentation
All character sets are implemented as Python frozensets and therefore support all frozenset operations. The simplest use case for character sets is checking whether a given character belongs in that set. For example, if we wanted to check if a given character is an Arabic letter, we can do the following: from camel_tools.utils.charsets import AR_LETTERS_CHARSET print('A' in AR_LETTERS_CHARSET) # False print('أ' in AR_LETTERS_CHARSET) # True
Top answer
1 of 4
15

Your code is correct as it works on my computer with both Python 2 and 3 (I'm on OS X):

~$ python -c 'print "تست"'
تست
~$ python3 -c 'print("تست")'
تست

The problem is with your terminal that can not output unicode characters. You could verify it by redirecting your output to a file like python3 my_file.py > test.txt and open the file using an editor.

If you are on Windows you could use a terminal like Console2 or ConEmu that renders unicode better than Windows prompt.

You may encounter errors with these terminals too because of wrong code-pages/encodings of Windows. There is a small python package that fixes them (sets them correctly):

1- Install this pip install win-unicode-console

2- Put this at the top of your python file:

try:
    # Fix UTF8 output issues on Windows console.
    # Does nothing if package is not installed
    from win_unicode_console import enable
    enable()
except ImportError:
    pass

If you got errors when redirecting to a file, you may fix it by settings io encoding:

On Windows command line:

SET PYTHONIOENCODING=utf-8

On Linux/OS X terminal:

export PYTHONIOENCODING=utf-8

Some points

  • There is no need to use u"aaa" syntax in python 3. Strings literals are unicode by default.
  • Default coding of files is UTF8 in python 3 so coding declaration comment (e.g. # -*- coding: utf-8 -*-) is not needed.
2 of 4
6

The output will depend basically on which platform&terminal you run your code. Let's examine the below snippet for different windows terminals running either with 2.x or 3.x:

# -*- coding: utf-8 -*-
import sys

def case1(text):
    print(text)

def case2(text):
    print(text.encode("utf-8"))

def case3(text):
    sys.stdout.buffer.write(text.encode("utf-8"))

if __name__ == "__main__":
    text = "چرا کار نمیکنی؟"

    for case in [case1, case2, case3]:
        try:
            print("Running {0}".format(case.__name__))
            case(text)
        except Exception as e:
            print(e)

        print('-'*80)

Results

Python 2.x

Sublime Text 3 3122

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    چرا کار نمیکنی؟--------------------------------------------------------------------------------

ConEmu v151205

    Running case1
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ
    --------------------------------------------------------------------------------
    Running case2
    'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
    --------------------------------------------------------------------------------
    Running case3
    'file' object has no attribute 'buffer'
    --------------------------------------------------------------------------------

Windows Command Prompt

    Running case1
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ
    --------------------------------------------------------------------------------

    Running case2
    'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
    --------------------------------------------------------------------------------

    Running case3
    'file' object has no attribute 'buffer'
    --------------------------------------------------------------------------------

Python 3.x

Sublime Text 3 3122

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    چرا کار نمیکنی؟--------------------------------------------------------------------------------

ConEmu v151205

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <undefined>
    --------------------------------------------------------------------------------
    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------
    Running case3
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ--------------------------------------------------------------------------------

Windows Command Prompt

    Running case1
    'charmap' codec can't encode characters in position 0-2: character maps to <unde
    fined>
    --------------------------------------------------------------------------------

    Running case2
    b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda
    \xa9\xd9\x86\xdb\x8c\xd8\x9f'
    --------------------------------------------------------------------------------

    Running case3
    ┌åÏ▒Ϻ ┌®ÏºÏ▒ ┘å┘à█î┌®┘å█îσ----------------------------------------------------
    ----------------------------

As you can see just using sublime text3 terminal (case3) worked alright. The other terminals didn't support persian. The main point here is, it depends which terminal & platform you're using.

Solution (ConEmu specific)

Modern terminals like ConEmu allows you to work with UTF8-Encoding as explained here, so, let's try:

chcp 65001 & cmd

And then running again the script against 2.x & 3.x:

Python2.x

Running case1
��را کار نمیکنی؟[Errno 0] Error
--------------------------------------------------------------------------------
Running case2
'ascii' codec can't decode byte 0xda in position 0: ordinal not in range(128)
--------------------------------------------------------------------------------
Running case3
'file' object has no attribute 'buffer'
--------------------------------------------------------------------------------

Python3.x

Running case1
چرا کار نمیکنی؟
--------------------------------------------------------------------------------
Running case2
b'\xda\x86\xd8\xb1\xd8\xa7 \xda\xa9\xd8\xa7\xd8\xb1 \xd9\x86\xd9\x85\xdb\x8c\xda\xa9\xd9\x86\xdb\x8c\xd8\x9f'
--------------------------------------------------------------------------------
Running case3
چرا کار نمیکنی؟--------------------------------------------------------------------------------

As you can see, now the output was succesfull with python3 case1 (print). So... moral of a fable... learn more about your tools and how to configure them properly for your use-cases ;-)

🌐
Stack Overflow
stackoverflow.com › questions › 35252138 › print-urdu-arabic-language-in-console-python
wing ide - Print Urdu/Arabic Language in Console (Python) - Stack Overflow
Additionally, you should make sure that unicode is enabled in your terminal/prompt window too. #!/usr/bin/env python # -*- coding: UTF-8 -*- arabic_words = u'لغت العربیه' print arabic_words
🌐
Stack Overflow
stackoverflow.com › questions › 67065873 › i-need-help-useing-this-code-to-print-arabic-words
python - i need help useing this code to print arabic words - Stack Overflow
testdata = "000000,000000,سالم,امعتيق,email@email,1 1 1990,female,en_En,new york,USA ,new yourk,https://www.example.com" def split(data, cols_before_addr=8, cols_after_addr=1): raw_cols = data.split(',') return raw_cols[:cols_before_addr] \ + ["\n".join(raw_cols[cols_before_addr:-cols_after_addr])] \ + raw_cols[-cols_after_addr:] print(split(testdata)) the problem is that i have arabic data and it shows like this
🌐
GitHub
github.com › asdoost › arabing
GitHub - asdoost/arabing: arabing (Arabic+string) is a Python library that extends the standard string module with full character-set support for languages written in Arabic-derived scripts — Arabic, Persian, Urdu, Pashto, Kurdish, Sindhi, Uyghur, Kashmiri, and Punjabi (Shahmukhi).
🔢 Digit conversion — convert between Latin, Arabic-Indic (٠١٢), and Extended Arabic-Indic (۰۱۲) digit systems · 🧹 Text normalization — strip diacritics (tashkeel), normalize Alef variants · 📚 Language registry — look up any language by name or ISO 639 code · 🪶 Zero dependencies — pure Python standard library only ... import arabing # Works exactly like the standard `string` module print(arabing.ascii_uppercase) # ABCDEFGHIJKLMNOPQRSTUVWXYZ print(arabing.capwords("hello world")) # Hello World # Arabic-script language support print(arabing.arabic_letters) # ابتثجحخد...ءأإؤئةى print(arabing.persian_digits) # ۰۱۲۳۴۵۶۷۸۹ print(arabing.urdu_punctuation) # ،؛؟٪٫٬ـ«»۔ print(arabing.pashto_all_characters) # letters + digits + punctuation combined
Author: asdoost
🌐
Sololearn
sololearn.com › en › Discuss › 135962 › can-python-work-with-arabic-letters-if-yeah-explain-please
Can python work with arabic letters? If yeah, explain please. | Sololearn: Learn to code for FREE!
December 20, 2016 - Arabic letters are encrypted in Unicode. To accomplish what you request we need to tell the python interpreter that we want to use different encoding and refer to the chart as followed below: TEST_FILE = codecs.open('test.ar', 'r',encoding='utf-8') p = re.compile(unicode('^????', 'utf-8'), re.U) for line in TEST_FILE: match = p.match(line) if match: print line.rstrip() print match.group().rstrip() TEST_FILE.close() Reference: Arabic letters - http://www.unicode.org/charts/PDF/U0600.pdf Let me know if you need any further assistance, Mr.
Top answer
1 of 1
6

As noted in the comments, it is not a trivial task display UTF-8 Arabic text correctly on an embedded device. You need to handle text direction, joining and character encoding.

I've had an attempt at this in the past for a PHP ESC/POS driver that I maintain, and was unable to get joined Arabic characters in native ESC/POS. However, I did end up settling on this workaround (PHP) that printed images instead.

The basic steps to working around this are:

  • Get an Arabic font, some text libraries, and an image library
  • Join ('reshape') the characters
  • Convert the UTF-8 to LTR (print) order, using the bidi text layout algorithm
  • Slap it on an image, right aligned
  • Print the image

To port this to python, I lent on this answer using Wand. The Python Image Library (PIL) was displaying diacritics as separate characters, making the output unsuitable.

The dependencies are listed in the comments.

#!/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 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

# Some variables
fontPath = "/usr/share/fonts/opentype/fonts-hosny-thabit/Thabit.ttf"
textUtf8 = u"بعض النصوص من جوجل ترجمة"
tmpImage = 'my-text.png'
printFile = "/dev/usb/lp0"
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 = fontPath
draw.font_size = 36
draw.text(printWidth, 22, textDisplay)
draw(im)
im.save(filename=tmpImage)

# Print an image with your printer library
printer = printer.File(printFile)
printer.set(align="right")
printer.image(tmpImage)
printer.cut()

Running the script gives you a PNG, and prints the same to a printer at "/dev/usb/lp0".

This is a standalone python-escpos demo, but I'm assuming that Odoo has similar commands for alignment and image output.

Disclaimer: I don't speak or write Arabic even slightly, so I can't be sure this is correct. I'm just visually comparing the print-out to what Google translate gave me.

🌐
Alraqmiyyat
alraqmiyyat.github.io › 2013 › 01-02.html
Python Functions for Arabic - al-Raqmiyyāt
February 1, 2013 - Some programs do not support Arabic, but might be crucial for research (for example, R). This function converts Arabic into a transliterated form that any program can process.
🌐
Grokbase
grokbase.com › t › python › python-list › 03cpx29af9 › print-arabic-characters
[Python] print arabic characters - Grokbase
December 22, 2003 - -- Gerardo Herzig Departamento de Proyectos Especiales e Internet Facultad de Medicina U.B.A. ... Ahmad wrote: I am a python newbie, I want to print on the console UTF-8 arabic characters. They print OK with print text.encode("UTF-8") BUT, the characters are printed LTR, not RTL (right to left).