Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Excel Sheets to End of Workbook

I am trying to add excel worksheets to the end of a workbook, reserving the first sheet for a summary.

import win32com.client

Excel = win32com.client.DispatchEx('Excel.Application')
Book = Excel.Workbooks.Add()

Excel.Visible = True

Book.Worksheets(3).Delete()
Book.Worksheets(2).Delete()

Sheet = Book.Worksheets(1)

Sheet.Name = "Summary"

Book.Worksheets.Add(After=Sheet)

Sheet = Book.Worksheets(2)

Sheet.Name = "Data1"

This code adds the new sheet to the left, despite using After=Sheet, and when I modify the sheet named "Data1", it overwrites the sheet named "Summary".

This is similar to this problem:

Adding sheets to end of workbook in Excel (normal method not working?)

but the given solutions don't work for me.

like image 788
PProteus Avatar asked Oct 23 '25 16:10

PProteus


1 Answers

Try using this by adding Before = None:

add = Book.Sheets.Add(Before = None , After = Book.Sheets(book.Sheets.count))
add.Name = "Data1"
like image 155
Cheng Avatar answered Oct 25 '25 07:10

Cheng



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!