Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating workbook and worksheet using openpyxl

I am trying to load an existing Excel file and create a new sheet inside that workbook, but my code is not working using openpyxl.

rb = load_workbook("C:\Raw_Dump.xlsx")
rb.create_sheet("Sheet2")
sheet1 = rb.worksheets[0]

Any help would be appreciated.

like image 622
GAURAV CHHABRA Avatar asked Nov 08 '25 17:11

GAURAV CHHABRA


1 Answers

You have to save the workbook to the same filename:

rb.save(r"C:\Raw_Dump.xlsx")

full working example:

import openpyxl

ws_name = r"Raw_Dump.xlsx"
rb = openpyxl.load_workbook(ws_name)
rb.create_sheet("Sheet2")
rb.save(ws_name)
like image 65
Jean-François Fabre Avatar answered Nov 10 '25 09:11

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!