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.
Python count lines in a file
- Firstly create one text file and write some lines.
- Then open the file in reading mode, by using open(file, mode) with the filename as a file, and “r” as a mode to open a file and read its contents.
- Create a list of the content where the elements are split whenever they encounter an “\n”.
- Use for loop and iterate the counter variable.
- Now, the variable counter is displayed with the final result.
file = open("item.txt", "r") Counter = 0 line_c = file.read() my_list = line_c.split("\n") for i in mylist: if i: Counter += 1 print("Tne number of lines in the file is: ", Counter)
After writing the above code (python count lines in a file), Ones you will print ” Counter “ then the output will appear as a “ The number of lines in the file is: 6 ”. Here, we can see that it counts the line from the file, and the value now presents in the variable counter is displayed.
You can refer to the below screenshot python count lines in a file
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
- Python File methods (With Useful Examples)
- Python read a file line by line example
In this Python tutorial, we learned how to read an excel file in Python, how to write to an existing file in Python, and python count lines in a file.