Openpyxl for cell.value and cell.internal_value error: 'int' object has no attribute 'internal_value'

Openpyxl for cell.value and cell.internal_value error: 'int' object has no attribute 'internal_value'

我正在尝试使用 openpyxl 打印 excel 中单元格的值。但是,我收到以下错误:'int' object has no attribute 'internal_value' when I attempt to do print(cell.internal_value) and 'int' object has no attribute 'value' when I try做 print(cell.value).

我做了以下事情:

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

有什么想法吗?

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

您应该首先尝试创建一个新的跨页sheet或加载一个现有的跨页,然后访问它的单元格。

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)