首页 > 解决方案 > 用于 cell.value 和 cell.internal_value 的 Openpyxl 错误:“int”对象没有属性“internal_value”

问题描述

我正在尝试使用在 excel 中打印单元格的值openpyxl。但是,我收到以下错误:当我尝试做时对象没有属性,当我尝试做'int'时对象没有属性。'internal_value'print(cell.internal_value)'int''value'print(cell.value)

我做了以下事情:

for cell in range(2,18):
    print(int(cell.value))
    print(cell.internal_value)
    print(cell.value)

有任何想法吗?

标签: pythonexcelcellopenpyxl

解决方案


您正在循环范围,因此cell实际上是一个整数而不是工作表的单元格,这就是您无法使用上述属性的原因。

您应该首先尝试创建一个新的电子表格或加载一个现有的电子表格,然后访问其单元格。

from openpyxl import load_workbook
wb = load_workbook(filename = 'name_of_your_spreadsheet.xlsx')
# Let's say you want to print the A column
for cell in wb['A2 A18']:
    print(int(cell.value)) #this might not work
    print(cell.internal_value)
    print(cell.value)

如果你想range在任何情况下使用你可以做这样的事情

from openpyxl import load_workbook
wb = load_workbook(filename = 'name_of_your_spreadsheet.xlsx')
# Let's say you want to print the A column
for i in range(2,18):
    cell = ws3.cell(column=0, row=i)
    print(int(cell.value)) #this might not work
    print(cell.internal_value)
    print(cell.value)

推荐阅读