Blog
How To Copy/Cut Rows and Columns Using xlwings?
Method
Row
#xlwings API
sht.api.Rows(‘2:2’).Copy()
#sht.api.Range(‘A7’).Select()
#sht.api.Paste()
#sht.api.Rows (‘2:3’).Select()
#bk.selection.api.Cut()
#sht.api.Range(‘A7’).Select()
#sht.api.Paste()
Column
#sht.api.Columns(‘A:A’).Copy()
#sht.api.Range(‘E1’).Select()
#sht.api.Paste()
#sht.api.Columns(‘B:C’).Select()
#bk.selection.api.Cut()
#sht.api.Range(‘F1’).Select()
#sht.api.Paste()
Sample Code
#Copying/Cutting Rows and Columns
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
#Row
#xlwings API
sht.api.Rows('2:2').Copy()
sht.api.Range('A7').Select()
sht.api.Paste()
#sht.api.Rows ('2:3').Select()
#bk.selection.api.Cut()
#sht.api.Range('A7').Select()
#sht.api.Paste()
#Column
#sht.api.Columns('A:A').Copy()
#sht.api.Range('E1').Select()
#sht.api.Paste()
#sht.api.Columns('B:C').Select()
#bk.selection.api.Cut()
#sht.api.Range('F1').Select()
#sht.api.Paste()
#bk.close()
#app.kill()
How To Select Rows and Columns Using xlwings?
Method
#Select a single row
#xlwings
sht[‘1:1’].select()
#xlwings API
#sht.api.Rows(1) .Select()
#sht.api.Range(‘1:1’).Select()
#sht.api.Range(‘a1’).EntireRow.Select()
#Select multiple consecutive rows
#xlwings
#sht[‘1:5’].select()
#sht[0:5,:].select()
#xlwings API
#sht.api.Rows(‘1:5’).Select()
#sht.api.Range(‘1:5’).Select()
#sht.api.Range(‘A1:A5’).EntireRow.Select()
#Select multiple non-consecutive rows
#xlwings
#sht.range(‘1:5,7:10’).select()
#xlwings API
#sht.api.Range(‘1:5,7:10’).Select()
#————————-
#Select a single column
#xlwings
#sht.range(‘A:A’).select()
#xlwings API
#sht.api.Columns(1).Select()
#sht.api.Columns(‘A’).Select()
#sht.api.Range(‘A:A’).Select()
#sht.api.Range(‘A1’).EntireColumn.Select()
#Select multiple consecutive columns
#xlwings
#sht.range(‘B:C’).select()
#sht[:,1:3].select()
#xlwings API
#sht.api.Columns(‘B:C’).Select()
#sht.api.Range(‘B:C’).Select()
#sht.api.Range(‘B1:C2’).EntireColumn.Select()
#Select multiple non-consecutive columns
#xlwings
#sht.range(‘C:E,G:I’).select()
#xlwings API
#sht.api.Range(‘C:E,G:I’).Select()
Sample Code
#Selecting Rows and Columns
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
#Select a single row
#xlwings
sht['1:1'].select()
#xlwings API
#sht.api.Rows(1) .Select()
#sht.api.Range('1:1').Select()
#sht.api.Range('a1').EntireRow.Select()
#Select multiple consecutive rows
#xlwings
#sht['1:5'].select()
#sht[0:5,:].select()
#xlwings API
#sht.api.Rows('1:5').Select()
#sht.api.Range('1:5').Select()
#sht.api.Range('A1:A5').EntireRow.Select()
#Select multiple non-consecutive rows
#xlwings
#sht.range('1:5,7:10').select()
#xlwings API
#sht.api.Range('1:5,7:10').Select()
#-------------------------
#Select a single column
#xlwings
#sht.range('A:A').select()
#xlwings API
#sht.api.Columns(1).Select()
#sht.api.Columns('A').Select()
#sht.api.Range('A:A').Select()
#sht.api.Range('A1').EntireColumn.Select()
#Select multiple consecutive columns
#xlwings
#sht.range('B:C').select()
#sht[:,1:3].select()
#xlwings API
#sht.api.Columns('B:C').Select()
#sht.api.Range('B:C').Select()
#sht.api.Range('B1:C2').EntireColumn.Select()
#Select multiple non-consecutive columns
#xlwings
#sht.range('C:E,G:I').select()
#xlwings API
#sht.api.Range('C:E,G:I').Select()
#bk.close()
#app.kill()
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()