Read Excel Files in Python with 21 Code Examples
How to Read an Excel File in Python (w/ 21 Code Examples)
Learning how to read an Excel file in Python can be a daunting task for novices and advanced users alike. But don't worry, with the help of this guide, you'll have everything you need to understand and master the task of reading an Excel file in Python.
In this guide, we'll go through 21 code examples that cover basic and advanced use cases when it comes to reading Excel files in Python. We'll cover the basics of how to open and read an Excel file in Python, then dive into more advanced topics such as manipulating and writing data back to your Excel files.
1. Installing Packages
Before we can start reading and writing our Excel files, we need to install the necessary packages. To do this, we'll use the pip install command in the terminal/command prompt. Here are the packages we'll install:
- openpyxl: A package for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files
- xlrd: A library for reading data from Excel files
- xlwt: A library for writing data to Excel files
The commands to install each of these packages are listed below:
$ pip install openpyxl
$ pip install xlrd
$ pip install xlwt
2. Opening an Excel File in Python
Once you've installed the necessary packages, you're ready to begin working with Excel files in Python. The first step is to open the Excel file you want to work with. To open an Excel file in Python, you can use either the openpyxl or xlrd package. Here is an example of how to open an Excel file using the openpyxl package:
import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
Here is an example of how to open an Excel file using the xlrd package:
import xlrd
wb = xlrd.open_workbook('example.xlsx')
3. Reading Data From an Excel File in Python
Now that you know how to open an Excel file in Python, let's move on to reading data from a file. To read data from an Excel file in Python, you can use the xlrd or openpyxl packages. Here is an example of how to read data from an Excel file using the xlrd package:
import xlrd
wb = xlrd.open_workbook('example.xlsx')
for sheet in wb.sheets():
for row in range(sheet.nrows):
for col in range(sheet.ncols):
value = sheet.cell(row, col).value
print(value)
Here is an example of how to read data from an Excel file using the openpyxl package:
import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
for sheet in wb:
for row in sheet.rows:
for cell in row:
value = cell.value
print(value)
4. Writing Data to an Excel File in Python
Once you've read data from an Excel file, you may want to write data back to it. To write data to an Excel file in Python, you can use either the xlwt or openpyxl packages. Here is an example of how to write data to an Excel file using the xlwt package:
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet('Sheet1')
value1 = 'Hello'
value2 = 'World'
sheet.write(0, 0, value1)
sheet.write(0, 1, value2)
wb.save('example.xls')
Here is an example of how to write data to an Excel file using the openpyxl package:
import openpyxl
wb = openpyxl.Workbook()
sheet = wb.active
value1 = 'Hello'
value2 = 'World'
sheet.cell(row=0, column=0).value = value1
sheet.cell(row=0, column=1).value = value2
wb.save('example.xlsx')
5. Conclusion
So there you have it - 21 code examples for reading and writing Excel files in Python. As you can see, the task of reading and writing Excel files in Python can be quite complex. But with the help of this guide, you now have all the knowledge you need to get started and master the task.