In this Python tutorial, we will discuss how to read Excel file (Python read excel file) and how to write to an excel file in python with examples.
Read Excel File in Python
To read an excel file in Python, we will use xlrd module to retrieve information from a spreadsheet. The command need to be installed is xlrd module. xlrd module is used to extract data from a spreadsheet.
import xlrd location = "C:\\Users\\ \\Documents\\demo.xlsx" wb = xlrd.open_workbook(location) sheet = wb.sheet_by_index(0) print(sheet.cell_value(0, 0))
After writing the above code (Read Excel File in Python), Ones you will print then the output will appear as a “ Name ”. Here, row 0 and column 0 data is extracted from the spreadsheet.
You can refer to the below screenshot Read Excel file in Python
write to excel python
To write to an excel file in Python, we can use xlsx module and we can perform multiple operations on the spreadsheet, also we can modify the data on python.
The command need to be installed is xlsxwriter module.
import xlsxwriter outWorkbook = xlsxwriter.Workbook("out.xlsx") outSheet = outWorkbook.add_worksheet() Name = ["John"] Salary =  outSheet.write("A1", "Names") outSheet.write("B1", "sal") outSheet.write(1, 0, Name) outSheet.write(1, 1, Salary) outWorkbook.close()
After writing the above code (Write Excel File in Python), Ones you will print then the output will appear as a “ Names John sal 12000 ”. Here, we can write the data on the spreadsheet by using the xlsxwriter module.
You can refer to the below screenshot Write Excel file in Python.
This is how we can write to existing excel file in Python.
You may like the following Python tutorials:
- Multiply in Python with Examples
- Python invalid literal for int() with base 10
- Python sort list of tuples
- Remove Unicode characters in python
- Comment lines in Python
- Python dictionary append with examples
- Check if a list is empty in Python
- Python convert list to string
- Python input and raw_input function
In this Python tutorial, we learned how to read an excel file in Python and how to write to an existing file in Python.