To read an Excel file in Python, you can use the pandas
library which provides a convenient interface to work with Excel files.
Here is a sample code that reads an Excel file named “example.xlsx” located in the current working directory:
import pandas as pd # Read Excel file into a pandas dataframe df = pd.read_excel("example.xlsx") # Print the first 5 rows of the dataframe print(df.head())
In the above code, we first import the pandas
library and then use the read_excel()
function to read the Excel file into a pandas dataframe named df
. We then print the first 5 rows of the dataframe using the head()
method.
Note that you might need to install the pandas
library using pip
before running the code. You can do this by running the following command in the terminal:
pip install pandas
Also, you need to have Microsoft Excel installed on your machine to read Excel files.
Excel Documents:
Excel is a spreadsheet program developed by Microsoft. It allows users to create, edit, and analyze data using cells arranged in rows and columns. Excel documents are files saved in a format that can be opened and edited using Microsoft Excel.
Excel documents typically have a .xlsx file extension, although earlier versions of Excel used different file extensions such as .xls or .xlsm.
Excel documents can contain multiple sheets, each of which is a separate tab in the workbook. Users can enter data into cells, apply formulas and functions to perform calculations, format cells to change their appearance, and create charts and graphs to visually represent data.
Excel is widely used in industries such as finance, accounting, and data analysis because of its powerful data manipulation and analysis features. Excel documents can also be exported or saved in different file formats, making it easy to share data with others who may not have Excel installed on their computers.
Creating a Workbook:
To create a new Excel workbook in Python, you can use the openpyxl
library.
Here is an example code that creates a new workbook and writes some data to it:
from openpyxl import Workbook # Create a new workbook workbook = Workbook() # Select the active worksheet worksheet = workbook.active # Write data to the worksheet worksheet['A1'] = 'Hello' worksheet['B1'] = 'World!' worksheet['C1'] = 123 # Save the workbook workbook.save('example.xlsx')
In the above code, we first import the Workbook
class from the openpyxl
library. We then create a new workbook object using the Workbook()
constructor.
Next, we select the active worksheet using the active
attribute of the workbook. We then write some data to the worksheet by assigning values to specific cells using the indexing syntax.
Finally, we save the workbook to a file named “example.xlsx” using the save()
method of the workbook object.
Note that you might need to install the openpyxl
library using pip
before running the code. You can do this by running the following command in the terminal:
pip install openpyxl
Also, you need to have Microsoft Excel installed on your machine to open the Excel file created by the code.
Reading from the openpyxl:
To read data from an existing Excel file using openpyxl
, you can use the load_workbook()
function to load the workbook, select a specific worksheet, and then read the data from specific cells.
Here’s an example code that reads data from an Excel file named “example.xlsx”:
from openpyxl import load_workbook # Load the workbook workbook = load_workbook('example.xlsx') # Select the worksheet worksheet = workbook.active # Read data from specific cells value1 = worksheet['A1'].value value2 = worksheet['B1'].value value3 = worksheet['C1'].value # Print the values print(value1) print(value2) print(value3)
In the above code, we first import the load_workbook
function from the openpyxl
library. We then load the Excel file named “example.xlsx” using the load_workbook()
function, which returns a workbook object.
Next, we select the active worksheet of the workbook using the active
attribute of the workbook. We then read the values of specific cells in the worksheet using indexing syntax.
Finally, we print the values of the cells using the print()
function.
Note that you need to have the openpyxl
library installed on your machine. You can install it using pip
by running the following command in the terminal:
pip install openpyxl
Also, you need to have Microsoft Excel installed on your machine to open the Excel file.