I have an .xlsx
file that contains the salary information of international workforce of an organization. I'm extracting some of the their details. All goes fine except for their salaries.
The salary values look like: £10,000
or €34000
and etc. In the excel file, the symbol was added via the Format Cell option. But my code reads only the actual numbers without the currency symbol.
The below is my code:
import openpyxl
wb = openpyxl.load_workbook("International_Employees.xlsx")
for ws in wb:
for row in iter_rows():
for cell in row:
if str(cell.value) == 'SALARY':
salary = "{1}".format(cell.value, cell.offset(0,1).value)
print(salary)
The output is simply: 10000
instead of £10,000
or £10000
How do I tell the program to read the symbol as well using openpyxl
?
You could use the number_format
property to find out, here is an example of how you could implement that:
c = ws['A1']
fmt = c.number_format
print(fmt) # >>> for me: #,##0.00\ "€"
symbol = re.search(r"[€£]", fmt)
if not symbol:
print("no currency")
else:
print(f"{c.value} {symbol.group(0)}") # >>> 1000 €
Update:
Update2:
for ws in wb:
for row in iter_rows():
for cell in row:
print(cell.value, cell.number_format)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.