有没有办法在openpyxl中加载excel文件而忽略数据类型?

问题描述 投票:0回答:1

我有一个 Excel 文件,单元格 C2 上的值为 20220831,单元格格式设置为日期。

当我尝试使用 openpyxl 加载文件时,我收到此警告:

C:\Users\joaquim.MASCARELL\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\worksheet\_reader.py:211: UserWarning: Cell C2 is marked as a date but the serial value 20220831 is outside the limits for dates. The cell will be treated as an error.
  warn(msg)

尝试获取单元格值会导致“#VALUE!”:

wb = xl.load_workbook(file)
sheet = wb["Sheet1"]
print(sheet["C2"].value)

>>#VALUE!

我想获取整数值“20220831”。

是否可以传递任何参数来忽略类型标记并仅返回存储在 xml 中的值?

python openpyxl
1个回答
0
投票

一个技巧,删除这一行即可

value = "#VALUE!"

在 C:\Users\joaquim.MASCARELL\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\worksheet_reader.py

                    except (OverflowError, ValueError):
                        msg = f"""Cell {coordinate} is marked as a date but the serial value {value} is outside the limits for dates. The cell will be treated as an error."""
                        warn(msg)
                        data_type = "e"
                        #value = "#VALUE!"
© www.soinside.com 2019 - 2024. All rights reserved.