Blog

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

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:

https://pypi.org/

Download the corresponding version and 32/64-bit installation file, then install it.