How To Create and Reference Worksheets Using xlwings?
Method
【xlwings】
bk.sheets.add(name=None, before=None, after=None)
【xlwings API】
bk.WorkSheets.Add(Before, After, Count, Type)
#xlwings
bk.sheets.add()
#Insert before the 2nd worksheet
#bk.sheets.add(before=bk.sheets(2))
#Insert after the 2nd worksheet
#bk.sheets.add(after=bk.sheets(2))
#Reference a worksheet
#bk.sheets[0]
#bk.sheets(1)
#sht=bk.sheets[‘Sheet1’]
#sht.name=’MySheet’
#xlwings API
#bk.api.Worksheets.Add()
#Insert before the 2nd worksheet
#bk.api.Worksheets.Add (Before=bk.api.Worksheets(2))
#Insert 3 worksheets at once, place them at the front
#bk.api.Worksheets.Add(Count=3)
#Create a new chart worksheet
#bk.api.Worksheets.Add(Type=xw.constants.SheetType.xlChart)
#Use the Sheets object
#sht=bk.api.Sheets.Add()
#Reference the worksheet
#sht=Worksheets(1)
#sht=Worksheets(‘Sheet1’)
Sample Code
#Creating and Referencing Worksheets
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings
bk.sheets.add()
#Insert before the 2nd worksheet
#bk.sheets.add(before=bk.sheets(2))
#Insert after the 2nd worksheet
#bk.sheets.add(after=bk.sheets(2))
#Reference a worksheet
#bk.sheets[0]
#bk.sheets(1)
#sht=bk.sheets['Sheet1']
#sht.name='MySheet'
#xlwings API
#bk.api.Worksheets.Add()
#Insert before the 2nd worksheet
#bk.api.Worksheets.Add (Before=bk.api.Worksheets(2))
#Insert 3 worksheets at once, place them at the front
#bk.api.Worksheets.Add(Count=3)
#Create a new chart worksheet
#bk.api.Worksheets.Add(Type=xw.constants.SheetType.xlChart)
#Use the Sheets object
#sht=bk.api.Sheets.Add()
#Reference the worksheet
#sht=Worksheets(1)
#sht=Worksheets('Sheet1')
bk.close()
app.kill()
