Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to get my cell values (ticker symbols) from the excel sheet, but cannot figure out how to specify rows/columns to use. The ws.iter_rows() method is not accepting the string. 

Here is the code I am using:

wb = openpyxl.load_workbook('/Users/SPAC_price_data.xlsx')

ws = wb['Common share price history']

for row in ws.iter_rows('A1:A36'):

    for cell in row:

            print(str(cell.value))

The error is:

    ---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-4-7fdf6dd1581e> in <module>

      1 if __name__ == "__main__":

----> 2      main()

<ipython-input-3-d2cdb2b8ed38> in main()

     16     wb = openpyxl.load_workbook('/Users/SPAC_price_data.xlsx')

     17     ws = wb['Common share price history']

---> 18     for row in ws.iter_rows('A1:A36'):

     19         for cell in row:

     20                 print(cell.value)

/Applications/anaconda3/lib/python3.8/site-packages/openpyxl/worksheet/worksheet.py in _cells_by_row(self, min_col, min_row, max_col, max_row, values_only)

    442 

    443     def _cells_by_row(self, min_col, min_row, max_col, max_row, values_only=False):

--> 444         for row in range(min_row, max_row + 1):

    445             cells = (self.cell(row=row, column=column) for column in range(min_col, max_col + 1))

    446             if values_only:

TypeError: 'str' object cannot be interpreted as an integer

1 Answer

0 votes
by (36.8k points)

The input parameters for the iter_rows() must be an integer. 

Replace:

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

With:

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

Learn Python for Data Science Course to improve your technical knowledge.

Browse Categories

...