1

Consider:

import openpyxl
wb = openpyxl.load_workbook('D:\excel.xlsx')
wb.get_sheet_names()

After this, I can see the worksheets (85) that the Excel file has. Now I want to go into each sheet, edit data in 2-3 cells (which is same for all the sheets) and then save the file.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Akhil Kodicherla
  • 61
  • 1
  • 1
  • 11
  • You'll get more help if you do at least try to write some code. – Charlie Clark Aug 18 '17 at 14:34
  • Possible duplicate of [Iteration over Worksheets, Rows, Columns](https://stackoverflow.com/questions/42974450/iteration-over-worksheets-rows-columns) – stovfl Aug 18 '17 at 17:58

2 Answers2

7

In your case I see the easiest is probably

import openpyxl

n = 0
wb = openpyxl.load_workbook('D:\excel.xlsx')
sheets = wb.sheetnames
ws = wb[sheets[n]]

Where n is the sheetnumber (0 is the first). Put that in a loop and increase n when you want to change the sheet.

jiipeezz
  • 235
  • 4
  • 10
2

When mentioning the path, use forward slash rather than backslash.

Get all sheet names. Find the sheetwise maximum row and column in a particular sheet

import openpyxl

workBook = load_workbook(filename='D:/excel.xlsx')
sheets = workBook.sheetnames
i = 1
for s_name in sheets:
    print(s_name)
    sheet = workBook[s_name]
    m_row = sheet.max_row
    m_col = sheet.max_column
    print(m_row)
    print(m_col)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Wow
  • 49
  • 3