问题
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