Openpyxl 'str' object cannot be interpreted as an integer

被刻印的时光 ゝ 提交于 2021-02-08 11:39:42

问题


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)

I am receiving an error:

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

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?


回答1:


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))

with

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


来源:https://stackoverflow.com/questions/58245369/openpyxl-str-object-cannot-be-interpreted-as-an-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!