无法使用Pandas或openpyxl在任何.xlsx文件的单元格中检测到EUR

时间:2018-10-22 07:29:21

标签: python python-3.x pandas openpyxl

enter image description here

我使用了熊猫read_excel,并且这些单元格仅获得了数字值,而没有获得“ EUR”(货币)。

然后我尝试使用openpyxl读取单元格值并输入“ int”而不是“ str”。

使用Libre calc或MS excel之类的excel编辑器,我们可以将货币转换为所需的输出,尽管,我需要找到一种方法来检测EUR为货币并将相应的值存储在数据库中。 / p>

我在哪里错了?我还有其他方法可以检测到它吗?

我在此处附上了excel工作表。Google sheets link..

1 个答案:

答案 0 :(得分:2)

可以将Excel单元格配置为单独显示“货币”。这只是视觉上的,内部是商店编号以及属于该单元格的格式元信息。您不能从单元格值中提取EUR,而是从格式字符串中提取EUR。

您可以查询每个单元格的格式并从中解析EUR

from openpyxl import Workbook, load_workbook

wb = load_workbook(r"stack_excel.xlsx")
ws = wb.worksheets[0]

for n in range(1,10):
    _cell = ws.cell(1,n)              # get a cell
    print((1,n), _cell.number_format) # read the number format of this cell

输出:

(1, 1) General
(1, 2) General
(1, 3) General
(1, 4) General
(1, 5) General
(1, 6) General
(1, 7) 0
(1, 8) [$EUR]\ 0
(1, 9) [$EUR]\ 0