Skip to main content
How to Write an Excel File in Python

How to Write an Excel File in Python

This python tutorial help to write to excel using python and xlrd lib. The xlrd module may be used to obtain data from a spreadsheet. It can be used to read, write, or alter data.

Sample Excel Sheet Data

Name         Age     Salary
Roji          32      1234
Adam          34      2134

The below command is used to be installed xlrd module. The xlrd module is used to extract data from a spreadsheet.

pip install xlrd

And at the start of our Python program it can be imported by including the below line:

import xlrd

Python code to read ezxcel data

import xlrd
wb = xlrd.open_workbook("employee.xlsx")
sheet = wb.sheet_by_index(0)
print(sheet.cell_value(0, 0))

The above will print row 0 and column 0 data , which is extracted from the spreadsheet.

How To Write Excel File

We can use the xlsx module in Python to write to an excel file and execute many operations on the spreadsheet, as well as edit the data.

import xlsxwriter
emp_wb = xlsxwriter.Workbook("employee.xlsx")
new_sheet = emp_wb.add_worksheet()
Name = ["Adam"]
Age = [34]
Salary = [12000]
new_sheet.write("A1", "Name")
new_sheet.write("B1", "Age")
new_sheet.write("C1", "Salary")

new_sheet.write(1, 0, Name[0])
new_sheet.write(1, 1, Age[0])
new_sheet.write(1, 2, Salary[0])
emp_wb.close()

Leave a Reply

Your email address will not be published. Required fields are marked *