如何使用openpyxl从python中的xlsx文件读取货币符号?

时间:2019-05-15 13:51:28

标签: python openpyxl xlsx

我有一个.xlsx文件,其中包含一个组织的国际劳动力的薪水信息。我正在提取其中的一些细节。除了工资以外,其他一切都很好。

薪水值如下:£10,000€34000等。在excel文件中,该符号是通过“格式单元格”选项添加的。但是我的代码只读取实际数字,而没有货币符号。

以下是我的代码:

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)

输出很简单:10000而不是£10,000£10000

如何告诉程序也使用openpyxl读取符号?

1 个答案:

答案 0 :(得分:0)

您可以使用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 €

更新:

d

Update2:

for ws in wb:
    for row in iter_rows():
        for cell in row:
            print(cell.value, cell.number_format)