Dosto
Dosto

Reputation: 1

Apply color to Arabic diacritics

I use Python language to entered Arabic text with diacritics, then try to get the same text as result but the diacritics to be in different color( red for example) and then save the result to rich text file.

The issue is that I receive the incorrect encoding in the rich file (the text is written in a language that is not understandable).

The result is shown in run window in PyCharm exactly as i want, but in the rich text file it shown like this:

 ط¥ظگظ†ظژظ‘ ط·ظژظ„ظژط¨ظژ ط§ظ„ظ’ط­ظژط§ط¬ظژط©ظگ ط£ظژظ‡ظ’ظˆظژظ†ظڈ ظ…ظگظ†ظ’ ط§ظ„ط§ط­ظ’طھظگظپظژط§ط¸ظگ ط¨ظگظ‡ظژط§

Can anyone help me with this problem.

Here is my Python code

from colorama import Fore, Style, init

# Initialize colorama to work on Windows
init()

# Function to color diacritics in red and create RTF content
def color_letter_to_rtf(text):
    # Start RTF document with Tahoma font
    rtf_content = r"{\rtf1\ansi\ansicpg1256\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset178 Tahoma;}}"
    rtf_content += r"{\colortbl ;\red255\green0\blue0;}"  # Define color table (red)

    # Add text to the RTF content
    for char in text:
        if char in ["َ", "ً", "ُ", "ٌ", "ِ", "ٍ", "ْ", "ّ"]:
            rtf_content += r"{\cf1 " + char + r"}"  # Apply red color for diacritics
        else:
            rtf_content += char  # Keep other characters as default

    rtf_content += r"}"  # End RTF document
    return rtf_content

# Define the Arabic sentence
sentence = "إِنَّ طَلَبَ الْحَاجَةِ أَهْوَنُ مِنْ الاحْتِفَاظِ بِهَا"

# Call the function to create RTF content
colored_sentence_rtf = color_letter_to_rtf(sentence)

# Save the RTF content to a file
with open("colored_sentence.rtf", "w", encoding="utf-8") as f:
    f.write(colored_sentence_rtf)

# Print the sentence with colored diacritics to console (optional)
colored_text = ""
for char in sentence:
    if char in ["َ", "ً", "ُ", "ٌ", "ِ", "ٍ", "ْ", "ّ"]:
        colored_text += f"{Fore.RED}{char}{Style.RESET_ALL}"
    else:
        colored_text += char

print(colored_text)
print("The colored sentence has been saved to 'colored_sentence.rtf'.")

Upvotes: 0

Views: 44

Answers (0)

Related Questions