Created
March 12, 2018 15:11
-
-
Save dustin-td/041a03ecef814b2747a21f1e8e965793 to your computer and use it in GitHub Desktop.
Python script to replace Western Arabic numerals with Eastern Arabic numerals in Excel sheet containing Arabic script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openpyxl | |
import sys, re | |
def ReplaceArabicNumerals(inbook,outbook,sheet): | |
wb = openpyxl.load_workbook(inbook) | |
sht = wb[sheet] | |
arabicCells = SearchForArabic(sht) | |
arabicNumerals = [(u'\u0660','0'), | |
(u'\u0661','1'), | |
(u'\u0662','2'), | |
(u'\u0663','3'), | |
(u'\u0664','4'), | |
(u'\u0665','5'), | |
(u'\u0666','6'), | |
(u'\u0667','7'), | |
(u'\u0668','8'), | |
(u'\u0669','9')] | |
for cell in arabicCells: | |
if cell.value is not None: | |
val = unicode(cell.value) | |
for k, v in arabicNumerals: | |
val = val.replace(v, k) | |
cell.value = val | |
wb.save(outbook) | |
def SearchForArabic(worksheet): | |
arabicCells = [] | |
pattern = re.compile(u'[\u0600-\u06FF]') | |
for row in worksheet: | |
for cell in row: | |
val = unicode(cell.value) | |
if pattern.search(val) is not None: | |
arabicCells.append(cell) | |
return arabicCells | |
if __name__ == '__main__': | |
ReplaceArabicNumerals(sys.argv[1], sys.argv[2], sys.argv[3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment