[英]How to read a currency symbol from an xlsx file in python using openpyxl?
I have an .xlsx
file that contains the salary information of international workforce of an organization. 我有一个
.xlsx
文件,其中包含组织的国际劳动力的薪资信息。 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. 工资值如下:
£10,000
或€34000
等。在excel文件中,符号是通过Format Cell选项添加的。 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
输出简单:
10000
而不是£10,000
£10000
或£10000
How do I tell the program to read the symbol as well using openpyxl
? 如何使用
openpyxl
告诉程序读取符号?
You could use the number_format
property to find out, here is an example of how you could implement that: 您可以使用
number_format
属性查找,以下是如何实现该示例的示例:
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: UPDATE2:
for ws in wb:
for row in iter_rows():
for cell in row:
print(cell.value, cell.number_format)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.