xlwings: python easy-to-use excel operation module

foreword

Due to the need for month-end reconciliation, it is necessary to compare the data of a certain column in multiple excels to find out the difference between the data of a certain column in the main table and similar columns in several other excel tables. Originally wps has a data difference comparison function, but it needs to be charged, so I can only write a function by myself, just use xlwings

process

1. Familiar with xlwings

what is xlwings

xlwings is a python module that can perform almost all operations on excel, and can also replace vba to reversely implement calling python in excel to process data. Support both mac and win

basic operation

  1. xlwings contains 4 levels of objects and operations: App (excel program) —> Book (excel document) —> Sheet (document page) —> Range (document cell)
    xlwings object structure
  2. Basic operation code example
# 示例1
import xlwings as xw
wb = xw.Book('test.xlsx') # 如果只是打开单个文档的情况下,完全可以用xw来替代app
sheet = wb.sheets[0]
a1 = sheet.range('A1').value
# 示例2
import xlwings as xw
app = xw.App()
book = app.books.open('test.xlsx')
sheet = app.books['test.xlsx'].sheets[0]
# 选择第一个单元格
range_obj = sheet.range('A1')
# 选取第一列
range_obj = sheet.range('A1').expand('down')
# 选取第一行
range_obj = sheet.range('A1').expand('right')
# 选取表格
range_obj = sheet.range('A1').expand('table')
# range的总行数
rows_count = range_obj.rows.count
# range的第一行行标
row_index = range_obj.row
# 返回range的列数
columns_count = range_obj.columns.count
# range的第一列列标
column_index = range_obj.column
# 设置range的颜色
range_obj.color=(255,255,255)
# 清除range的背景色
range_obj.color=None
# 获取公式或者输入公式
range_obj.formula='=SUM(B1:B5)'
# 存储单个数据
range_obj.value=666
# 横向存储多个数据
sheet_obj.range('A1').value=[1,2,3]  #等同sheet_obj.range('a1:c1').value = [1,2,3]
#纵向存储多个数据
sheet_obj.range('A1').options(transpose=True).value=[1,2,3]
#直接存储多维数据
# 将2x2表格,储存在A1:B2中,如第一行1,2,第二行3,4
sheet_obj.range('A1').options(expand='table').value=[[1,2],[3,4]] 
# 同上
sheet_obj.range('A1').expand('table').value = [[1,2],[3,4]]
# 读取数据的方法一致,为赋值左侧方法

2. Having a problem

The excel version is too low to be fully automated. Due to the environment (windows, the installed excel version is very old, some formats are poorly supported, errors are reported, human intervention is required, and the efficiency is low), the main force usually uses wps, so I plan to call wps directly

3. Solve the problem

  1. First searched on Baidu and found that there are many solutions, many of which are directly changing the source code of the library. After trying it, there is no problem at all. But I always feel that this kind of solution is unsustainable. Maybe one day the module will be updated, and I will have to change it back
  2. After searching carefully, I found an external introduction without modifying the source code: python uses xlwings to operate wps
# 运行程序前,传入自定义的impl就可以了
_xl = COMRetryObjectWrapper(DispatchEx("ket.Application")) # 标识用wps运行
impl = xw._xlwindows.App(add_book=False, xl=_xl)
app = xw.App(visible=False, add_book=False, impl=impl) # visible可以控制是否展示wps

Summarize

xlwings, the operation of excel is super simple. Use the program to open the document to operate the excel document. After the program exits, the modification will be automatically saved without special operation.

Reference and Extension

  1. xlwings
  2. xlwings official documentation - English

Guess you like

Origin blog.csdn.net/qinmin1/article/details/126467699