Openpyxl'str'对象不能解释为整数 [英] Openpyxl 'str' object cannot be interpreted as an integer

查看:97
本文介绍了Openpyxl'str'对象不能解释为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行一段简单的代码(Python 3.6)将excel工作表中的一系列单元格转换为一个列表.这是代码:

I am trying to run a simple piece of code (Python 3.6) to convert a range of cells in excel worksheet into a list. Here is the code:

import openpyxl

wb_d = openpyxl.load_workbook('example.xlsx')
ws = wb_d.active
# iterate through all rows in specific column openpyxl
mylist = []
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
    for cell in row:
        mylist.append(cell.value)
print(mylist)

我收到一个错误:

Traceback (most recent call last):
  File "PycharmProjects/Excel/list_generator.py", line 7, in <module>
    for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
  File "venv\Excel\lib\site-packages\openpyxl\worksheet\worksheet.py", line 438, in _cells_by_row
    for row in range(min_row, max_row + 1):
TypeError: 'str' object cannot be interpreted as an integer

我也尝试将单元格的范围定义为"A1:A10",并且还收到一个错误.任何人都可以请教一下代码有什么问题吗?

I tried also to define the range of cell as 'A1:A10' and also received an error. Could anyone please advice what is the problem with the code?

推荐答案

The input parameters for iter_rows() must be integers. So replace

for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row))

使用

for row in ws.iter_rows(ws.min_row,ws.max_row)

这篇关于Openpyxl'str'对象不能解释为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆