简体   繁体   中英

How to switch between sheets in excel openpyxl python to make changes

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.

I am very new to python and i need help. Thanks

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.

I hope below code will be help. When mentioning the path use front slash rather than back slash.

Get All Sheet Name. Find sheetwise max row and column in 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM