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)
问题是 var t 中的数字也发生了翻转 喜欢 48 -> 84 我需要一个解决方案,请
您可以通过将字符串拆分为单词、检查每个单词是否为数字并仅反转非数字部分来避免此问题
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) # Keep numbers as they are
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 = "مرحبا 123 456"
reversed_text = reverse_arabic_string_with_numbers(t)
print(reversed_text)