Use the Python library xlwings to operate EXCEL
Using Python's library (also known as module) xlwings can be said to be a powerful tool for processing Excel data. Xlwings is open source and free, which can easily read and write data in Excel files, and can modify the format of cells.
xlwings documentation https://docs.xlwings.org/en/stable/
xlwings Chinese document https://www.kancloud.cn/gnefnuy/xlwings-docs/1127450
To use xlwings, you need to install xlwings. You can use the following command in cmd to install:
pip install xlwings
[For details, please refer to the installation, uninstallation and viewing of Python third-party modules (libraries, packages) and common problem resolution https://blog.csdn.net/cnds123/article/details/104393385 ]
To test whether the installation is successful, type in the IDLE shell:
import xlwings as xw
See the picture below:
If no error is reported, the installation of xlwings is successful.
If an error is reported, it prompts No module named'pywintypes'
First close IDLE shell, execute in cmd: pip install pypiwin32
Start the IDLE shell again and enter import xlwings as xw to test
After the xlwings library is successfully installed, if you want to use it in python code, first import the xlwings library:
import xlwings
or
import xlwings as xw
[Python import statement syntax
Format 1:
import module name [as alias]
Instructions: 1) Import the entire module. 2) The [as alias] part is optional, and the alias is used to simplify reference. 3) Use the format of the members in the imported module: module name [or alias]. member
Members include attributes (variables) or functions.
Format 2:
from module name import member name or *
Description: 1) Import a member in the module, * represents all members.
If importing the module in the package, change the module name in the above syntax to the package name. Module name
For details, please refer to https://blog.csdn.net/weixin_38256474
】
The basic object hierarchy of xlwings, see the following figure:
New application (or called open Excel program, an application xlwings program ): app = xw.App (visible = True, add_book = False)
Add workbook (book, workbook): excel file (excel program): wb = app.books.add()
Save the workbook: wb.save(f'D:\\example\\FileName.xlsx')
Reference workbook
The workbook should be opened first before referencing the workbook with the following statement:
wb.=xw.books['The name of the workbook']
For the active workbook, use the following statement to quote
wb=xw.books.active
Refer to the sheet in the workbook:
sht=xw.books['The name of the workbook'].sheets['The name of the sheet']
or
wb=xw.books['The name of the workbook']
sht=wb.sheets[sheet's name]
Reference cell:
Suppose cell A1 is referenced
rng=xw.books['The name of the workbook'].sheets['The name of the sheet']
or
sht=xw.books['The name of the workbook'].sheets['The name of the sheet']
rng = sht.range ('A1')
Special reminder: The cell method on the worksheet (sheet) is more flexible and diverse. We will introduce these first, and we will introduce them later.
Close the workbook: wb.close()
Close the application (Excel program): app.quit()
Example 1. In the directory "D:\example\exercise 01" (if this directory does not exist, please create it first), create a new workbook named test.xlsx, enter "ABC123" in cell A1 of sheet1, and save Close and exit the Excel program. code show as below:
import xlwings as xw #导入xlwings
app=xw.App(visible=True,add_book=False) #新建应用(或叫打开Excel程序)
wb=app.books.add() # 新建的工作簿wb,下一行对wb的sheet1的A1单元格赋值
wb.sheets['sheet1'].range('A1').value='ABC123'
wb.save(f'D:\\example\\练习01\\testA.xlsx') #保存工作簿
wb.close()
app.quit()
As can be understood from the above example, the general steps of using the xlwings library to read and write excel:
Import xlwings first, then
Application -> Workbook -> Worksheet -> Read and Write Range (range) -> Close Workbook -> Close Application
Example 2, open the saved testA.xlsx , in sheet1 table A2 cell entry , " Oh " , then save and close exit Excel program . code show as below:
import xlwings as xw #导入xlwings
app=xw.App(visible=True,add_book=False) #新建应用(或叫打开Excel程序)
wb=app.books.open(f'D:\\example\\练习01\\testA.xlsx') # 打开工作簿(workbook)
wb.sheets['sheet1'].range('A2').value='呵呵' #对工作簿的sheet1的A1单元格赋值
wb.save()
wb.close()
app.quit()
Example 3. Open the saved testA.xlsx, insert the values shown in the figure below in sheet1, then save and close, and exit the Excel program. code show as below:
import xlwings as xw #import xlwings
app=xw.App(visible=True,add_book=False) #New application (or open Excel program)
wb=app.books.open(f'D:\\example\\exercise01\\testA.xlsx') # open the workbook (workbook)
sht = wb.sheets['sheet1'] # 页sheet1
# Insert rows and columns at the same time
sht.range('a6').expand('table').value = [['a','b','c'],['d','e','f'],['g','h','i']]
# Generate files in the current directory
wb.save(f'D:\\example\\exercise 01\\testA.xlsx')
wb.close()
app.quit()
to be continued