[英]How to read a currency symbol from an xlsx file in python using openpyxl?
我有一個.xlsx
文件,其中包含組織的國際勞動力的薪資信息。 我正在提取他們的一些細節。 一切都很好,除了他們的薪水。
工資值如下: £10,000
或€34000
等。在excel文件中,符號是通過Format Cell選項添加的。 但我的代碼只讀取沒有貨幣符號的實際數字。
以下是我的代碼:
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
或£10000
如何使用openpyxl
告訴程序讀取符號?
您可以使用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 €
更新:
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.