Blog
How To Hide and Show Worksheets Using xlwings?
Method
Normal hide
#xlwings API
bk.api.Sheets(‘Sheet1’).Visible = False
#bk.api.Sheets(‘Sheet1’).Visible = xw.constants.SheetVisibility.xlSheetHidden
#bk.api.Sheets(‘Sheet1’).Visible = 0
Deep hide
#xlwings API
#bk.api.Sheets(‘Sheet1’).Visible=xw.constants.SheetVisibility.xlSheetVeryHidden
#bk.api.Sheets(‘Sheet1’).Visible=2
Unhide
#xlwings
#bk.sheets(‘Sheet1’).visible = True
#bk.sheets(‘Sheet1’).visible = 1
#xlwings API
#bk.api.Sheets(‘Sheet1’).Visible = True
#bk.api.Sheets(‘Sheet1’).Visible = xw.constants.SheetVisibility.xlSheetVisible
#bk.api.Sheets(‘Sheet1’).Visible = 1
#bk.api.Sheets(‘Sheet1’).Visible = -1
Sample Code
#Hiding and Showing 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
sht2=bk.sheets.add()
#Normal hide
#xlwings API
#bk.api.Sheets('Sheet1').Visible = False
#bk.api.Sheets('Sheet1').Visible = xw.constants.SheetVisibility.xlSheetHidden
#bk.api.Sheets('Sheet1').Visible = 0
#Deep hide
#xlwings API
bk.api.Sheets('Sheet1').Visible=xw.constants.SheetVisibility.xlSheetVeryHidden
#bk.api.Sheets('Sheet1').Visible=2
#Unhide
#xlwings
bk.sheets('Sheet1').visible = True
#bk.sheets('Sheet1').visible = 1
#xlwings API
#bk.api.Sheets('Sheet1').Visible = True
#bk.api.Sheets('Sheet1').Visible = xw.constants.SheetVisibility.xlSheetVisible
#bk.api.Sheets('Sheet1').Visible = 1
#bk.api.Sheets('Sheet1').Visible = -1
#bk.close()
#app.kill()
How To Activate, Copy, Move, and Delete Worksheets Using xlwings?
Method
Activate a worksheet
#xlwings
bk.sheets[1].activate()
#bk.sheets[1].select()
#xlwings API
#bk.api.Worksheets(2).Activate()
#bk.api.Worksheets(2).Select()
Copy worksheets
#xlwings API
#bk.api.Sheets(‘Sheet1’).Copy(Before=bk.api.Sheets(‘Sheet2’))
#bk.api.Sheets(‘Sheet1’).Copy(After=bk.api.Sheets(‘Sheet2’))
Copy across workbooks
#bk.api.Sheets(‘Sheet1’).Copy(Before=bk2.api.Sheets(‘Sheet2’))
#bk.api.Sheets(‘Sheet1’).Copy(After=bk2.api.Sheets(‘Sheet2’))
Move worksheets
#xlwings API
#bk.api.Sheets(‘Sheet1’).Move(Before=bk.api.Sheets(‘Sheet3’))
#bk.api.Sheets(‘Sheet1’).Move(After=bk.api.Sheets(‘Sheet3’))
#bk.Sheets([‘Sheet2’, ‘Sheet3’]).Move(Before=bk.Sheets(1))
Delete worksheets
#xlwings
#bk.sheets(‘Sheet1’).delete()
#bk.sheets([‘Sheet2’, ‘Sheet3’]).delete()
#xlwings API
#bk.api.Sheets(‘Sheet1’).Delete()
#bk.api.Sheets([‘Sheet2’, ‘Sheet3’]).Delete()
Sample Code
#Activating, Copying, Moving, and Deleting Worksheets
import xlwings as xw #Import the xlwings package
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active
sht2=bk.sheets.add()
sht3=bk.sheets.add()
sht4=bk.sheets.add()
#Activate a worksheet
#xlwings
bk.sheets[1].activate()
#bk.sheets[1].select()
#xlwings API
#bk.api.Worksheets(2).Activate()
#bk.api.Worksheets(2).Select()
#Copy worksheets
#xlwings API
#bk.api.Sheets('Sheet1').Copy(Before=bk.api.Sheets('Sheet2'))
#bk.api.Sheets('Sheet1').Copy(After=bk.api.Sheets('Sheet2'))
#Copy across workbooks
#bk.api.Sheets('Sheet1').Copy(Before=bk2.api.Sheets('Sheet2'))
#bk.api.Sheets('Sheet1').Copy(After=bk2.api.Sheets('Sheet2'))
#Move worksheets
#xlwings API
#bk.api.Sheets('Sheet1').Move(Before=bk.api.Sheets('Sheet3'))
#bk.api.Sheets('Sheet1').Move(After=bk.api.Sheets('Sheet3'))
#bk.Sheets(['Sheet2', 'Sheet3']).Move(Before=bk.Sheets(1))
#Delete worksheets
#xlwings
#bk.sheets('Sheet1').delete()
#bk.sheets(['Sheet2', 'Sheet3']).delete()
#xlwings API
#bk.api.Sheets('Sheet1').Delete()
#bk.api.Sheets(['Sheet2', 'Sheet3']).Delete()
#bk.close()
#app.kill()
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()
How To Reference, Activate, Save, and Close Workbooks Using xlwings?
Method
Reference a workbook
#xlwings
bk=xw.books[0]
#bk2=xw.books(2)
#pid=xw.apps.keys()
#app2=xw.apps[pid[0]]
Activate a workbook
#xlwings
#xw.books(1).activate()
#xw.books.active.name
#xlwings API
#app.api.Workbooks(1).Activate()
#app.api.ActiveWorkbook.Name
Save the workbook
#xlwings
bk.save()
#xlwings API
#bk=app.api.Workbooks(1)
#bk=app.api.Workbooks(“Workbook1”)
#bk.SaveAs(r”D:\test.xlsx”)
#bk.SaveCopyAs(r”D:\test.xlsx”)
Close the workbook
#xlwings
bk.close()
#xlwings API
app.api.Workbooks(1).Close()
Sample Code
#Reference, activate, save, and close a workbook
import xlwings as xw #Import the xlwings package
app=xw.App()
app.books.add()
#xlwings
bk=xw.books[0]
#bk2=xw.books(2)
#pid=xw.apps.keys()
#app2=xw.apps[pid[0]]
#Activate a workbook
#xlwings
#xw.books(1).activate()
#xw.books.active.name
#xlwings API
#app.api.Workbooks(1).Activate()
#app.api.ActiveWorkbook.Name
#Save the workbook
#xlwings
#bk.save()
#xlwings API
#bk=app.api.Workbooks(1)
#bk=app.api.Workbooks("Workbook1")
#bk.SaveAs(r"D:\test.xlsx")
#bk.SaveCopyAs(r"D:\test.xlsx")
#Close the workbook
#xlwings
bk.close()
#xlwings API
#app.api.Workbooks(1).Close()
app.kill()
How To Create and Open Workbooks Using xlwings?
Method
- Creating a Workbook:
bk=app.Workbooks.Add()
bk=app.Workbooks.Add(constants.xlWBATChart)
bk=app.Workbooks.Add(r’C:\1.xlsx’)
- Opening a Workbook:
bk=app.Workbooks.Open(r’D:\test.xlsx’)
bk=app.Workbooks.Open(root+r’/test.xlsx’)
#Create a workbook
#xlwings
bk=xw.books.add()
#Or
#bk=xw.Book()
#Or
#app=xw.App()
#bk=xw.books.active
#xlwings API
#app=xw.App()
#bk=app.api.Workbooks.Add()
#Open a workbook
#xlwings
#bk=xw.books.open(r’D:\1.xlsx’) ”d:/1.xlsx’
#xlwings API
#bk=app.api.Workbooks.Open(r’D:\1.xlsx’)
Sample Code
#Creating and Opening Workbooks
import xlwings as xw #Import the xlwings package
#Create a workbook
#xlwings
#app=xw.App()
#bk=xw.books.add()
#Or
#bk=xw.Book()
#Or
#app=xw.App()
#bk=xw.books.active
#xlwings API
#app=xw.App()
#bk=app.api.Workbooks.Add()
#Open a workbook
#xlwings
app=xw.App()
bk=app.books.open(r'data.xlsx')
#xlwings API
#bk=app.api.Workbooks.Open(r'data.xlsx')
bk.close()
app.kill()
Other Common Properties of Application Object
Method
#Refresh screen #xlwings app.screen_updating=False #Show warnings #xlwings #app.display_alerts=False #Call worksheet functions #xlwings #app.api.WorksheetFunction.CountIf(app.api.Range(‘B2:F5′),’>8′)Sample Code
#Other commonly used properties
import xlwings as xw #Import the xlwings package
app=xw.App()
#Refresh screen
#xlwings
app.screen_updating=False
#Show warnings
#xlwings
#app.display_alerts=False
#Call worksheet functions
#xlwings
#app.api.WorksheetFunction.CountIf(app.api.Range('B2:F5'),'>8')
#app.kill()
Application Object: Properties for Position, Size, Title, Visibility, and Status
Method
#xlwings API
app.api.Left #Location
#app.api.Top
#app.api.Width #Size
#app.api.Height
#app.api.Caption #Title
#app.api.Visible #Visibility
#app.api.WindowState #Status
#app.api.WindowState=xw.constants.WindowState.xlMaximized
Sample Code
#Location, size, title, visibility, and status properties
import xlwings as xw #Import the xlwings package
app=xw.App()
#xlwings API
app.api.Left #Location
#app.api.Top
#app.api.Width #Size
#app.api.Height
#app.api.Caption #Title
#app.api.Visible #Visibility
#app.api.WindowState #Status
#app.api.WindowState=xw.constants.WindowState.xlMaximized
app.kill()
Two Ways to Use xlwings
Method
xlwings method, using new syntax
sht.range(‘A1’).value=10
xlwings API method, using VBA-style class syntax
sht.api.Range(‘A1’).Value=10
sht.api returns a COM object.
Sample Code
#Two Programming Approaches with the xlwings Package
import xlwings as xw #Import the xlwings package
app=xw.App() #Create an Excel application
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#xlwings
sht.range('A1').value=10
#xlwings API
#sht.api.Range('A1').Value=10
bk.close()
app.kill()
General Process of Using xlwings
Method
Import xlwings package
import xlwings as xw
Create or get main objects
app=xw.App()
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active sheet
Data processing
sht.range(‘A1’).value=10
Save the workbook, close it and exit
bk.close()
app.kill()
Sample Code
#General Process of Using the xlwings Package
import xlwings as xw #Import the xlwings package
app=xw.App() #Create an Excel application
bk=app.books.active #Get the active workbook
sht=bk.sheets.active #Get the active worksheet
#Process data
sht.range("A1").value=10
#Close the workbook
bk.close()
#Exit the application
app.kill()

xlwings Packages and It's Installation
Comparison of Python Packages Related to Excel
|
Python Package |
Description |
|
xlrd |
Supports reading `.xls` and `.xlsx` files |
|
xlwt |
Supports writing `.xls` files |
|
OpenPyXl |
Supports reading and writing `.xlsx`, `.xlsm`, `.xltx`, `.xltm` files, supports the Excel object model, and does not depend on Excel |
|
XlsxWriter |
Supports writing `.xlsx` files, supports VBA |
|
win32com |
Encapsulates all Excel objects used by VBA |
|
comtypes |
Encapsulates all Excel objects used by VBA |
|
xlwings |
Rewraps Win32COM, supports mixed programming with VBA, and supports data type conversion between various data types |
|
pandas |
Supports reading and writing `.xls` and `.xlsx` files, provides various data processing functions, simpler processing, and faster speed |
OpenPyXl(Does not depend on Excel)
win32com:xlwings predecessor, encapsulates VBA object model, Windows platform
xlwings:Powerful, platform-independent
pandas:Based on NumPy, fast, data cleaning
Excel Objects and Their Hierarchy
Application Object
Workbooks Collection Object
Workbook Object
Worksheets Collection Object
Worksheet Object
Range Obejct
Installation of xlwings Package
- Install directly online. In PowerShell, type:
pip install xlwings
Press Enter.
- For offline installation, visit the URL:
Download the corresponding version and 32/64-bit installation file, then install it.