if cell is not None and cell.data_type == 'f': AttributeError: 'int' object has no attribute 'data_type'
时间: 2023-09-20 09:12:39 浏览: 126
非常抱歉,我的回答有误。在openpyxl中,单元格对象的属性不是`data_type`,而是`data_type`。所以代码应该修改为:
```
# 显示第一个工作表的内容
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
row_values = []
for cell in row:
if cell is not None and cell.data_type == 'f':
row_values.append(cell.value)
else:
row_values.append(cell if cell is not None else "")
if all(not bool(cell) for cell in row_values):
continue
treeview1.insert("", tk.END, values=row_values)
```
这样,就可以避免将公式显示在表格中了。
相关问题
AttributeError: 'int' object has no attribute 'data_type'報這個
抱歉,我之前的回答有误,cell 对象没有 data_type 属性。您可以使用 openpyxl 库中的 is_date_format 方法来判断单元格是否为日期格式,如果是则将日期格式转换为字符串。以下是修改后的代码:
```python
import openpyxl.utils.cell as cell_utils
for row in self.record_sheet.iter_rows(min_row=2, values_only=True):
row_values = []
for cell in row:
if cell is not None:
if cell_utils.is_date_format(cell.number_format): # 判断单元格是否为日期格式
cell_value = cell.value.strftime('%Y-%m-%d %H:%M:%S') # 将日期格式转换为字符串
elif cell.data_type == 'f': # 判断单元格是否为公式
cell_value = cell.value # 获取公式
cell_value = self.record_sheet[cell.coordinate].value # 获取公式计算结果
else:
cell_value = str(cell)
else:
cell_value = ""
row_values.append(cell_value)
if all(not bool(cell) for cell in row_values):
continue
treeview1.insert("", tk.END, values=row_values)
```
同样的,您也可以将相同的代码添加到第二个工作表的 for 循环中。
AttributeError: 'NoneType' object has no attribute 'int64'
This error occurs when we try to access the attribute 'int64' on an object that is of type 'NoneType'.
For example, consider the following code:
```
x = None
print(x.int64)
```
Since the variable 'x' is assigned the value of 'None', it has no attributes. Therefore, when we try to access the attribute 'int64', we get an AttributeError as NoneType object has no attribute 'int64'.
To resolve this error, we need to make sure that the object we are trying to access the attribute on is not of type 'NoneType'.
阅读全文