arabic letters with numpers python

63 Views Asked by At
for i in t.split('\n'):                
    arabic_string = arabic_reshaper.reshape(i)
    arabic_string = arabic_string[::-1]
    w = pdf.get_string_width(arabic_string) + 6
    pdf.cell(w, 9, arabic_string, 0, 1, 'C', 0)

the problem is that numpers in the var t is flipt too like 48 -> 84 I need a soloution pls

1

There are 1 best solutions below

3
tousif Noor On

You can avoid this issue by splitting the string into words, checking if each word is numeric, and reversing only non-numeric parts

import arabic_reshaper
from bidi.algorithm import get_display

    def reverse_arabic_string_with_numbers(t):
        reversed_lines = []
        for line in t.split('\n'):
            words = line.split()
            reversed_words = []
            for word in words:
                if word.isnumeric():
                    reversed_words.append(word)
                else:
                    arabic_word = arabic_reshaper.reshape(word)
                    arabic_word = arabic_word[::-1]
                    arabic_word_display = get_display(arabic_word)
                    reversed_words.append(arabic_word_display)
            reversed_lines.append(' '.join(reversed_words))
        return '\n'.join(reversed_lines)

#usage:
t = "مرحبا 48"
reversed_text = reverse_arabic_string_with_numbers(t)