One, xlwings overview
1.xlwings features
- xlwings can read and write data in Excel files very conveniently, and can modify the cell format
- Can be seamlessly connected with matplotlib and pandas
- You can call a program written in VBA in an Excel file, or you can make VBA call a program written in Python.
- Open source and free, always updated
2.xlwings basic object
Object name | Object description | Creation method | Remarks |
---|---|---|---|
App | Open the Excel program | 1. Create the app object import xlwings as xw Default setting: the program is visible, only open without creating a new workbook, and the screen update is closed 2. Open an excel file (the following two writing methods can be used) wb = app.books("xxx.xlsx") wb = app.books.open("xxx.xlsx") |
To open an .xlsx file, you can either create an App object first, or |
Book | Open workbook | >>> import xlwings as xw >>> wb = xw.Book() # This will create a new workbook >>> wb = xw.Book('FileName.xlsx') # Connect to an existing file in the current working directory >>> wb = xw.Book(r'C:\path\to\file.xlsx') # On Windows: Use raw strings to escape backslashes |
|
Sheet | Open the specified sheet | >>> sht = wb.sheets['Sheet1'] >>> sht = wb.sheets[1] |
The subscript of the sheet starts from 1, not from 0 |
Range | Read cell value | >>> sht.range('A1').value = 'Foo 1' >>> sht.range('A1').value 'Foo 1' |
Reference article: