This tutorial will walk through how to use Python xlwings to open Excel files. Xlwings is THE best python library for Excel automation. This is part of the xlwings tutorial series – where I’ll be covering most features of xlwings and walk through several examples on how to use Python and xlwings to fully automate Excel files.
Required Library
For this tutorial, we’ll be using only the xlwings library. If you need to install it:
pip install xlwings
Also, note that in order to use the xlwings library, we must have the Microsoft Excel program installed on our computer.
Xlwings is a smart wrapper of the pywin32 library (for Windows) or appscript (for Mac). That’s why we need to have Excel on the computer.
Excel Workbook Object
The Book constructor will create an Excel workbook. When we create a Book object, the Excel program will actually open up. This is different from other libraries such as openpyxl or xlsxwriter, where no actual Excel file will be open, and all the “workbooks” are created inside your computer memory.
Create & Open A New Excel Workbook
To create and open an empty Excel workbook, we just need to call the Book() constructor, without passing any arguments into it.
import xlwings as xw
wb = xw.Book()
Xlwings Open Existing Excel Workbook
To open up an existing Excel workbook, we just need to simply give the Book() constructor a file path. Again, note this will actually open the file inside the Excel program. If you see Excel opening up, don’t panic!
wb = xw.Book('your_excel_file_here.xlsx')
Additional Resources
How to Create Excel File in Python
The post Use Python Xlwings to Open Excel Files appeared first on Python In Office.