Skip to content

Instantly share code, notes, and snippets.

@dustin-td
Created March 12, 2018 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustin-td/041a03ecef814b2747a21f1e8e965793 to your computer and use it in GitHub Desktop.
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
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