Python 3 Script Create and Write Excel File Using xlsxwriter Module Full Project For Beginners

Python 3 Script Create and Write Excel File Using xlsxwriter Module Full Project For Beginners

 

Welcome folks today in this blog post we will be creating excel file using xlsxwriter module. All the source code of the project is given below.

 

 

Get Started

 

 

In order to get started you need to install xlsxwriter module by pip command in python

 

pip install xlsxwriter

 

After installing it you need to make a app.py file and copy paste the following code

 

app.py

 

 

# import xlsxwriter module 
import xlsxwriter 

# Workbook() takes one, non-optional, argument 
# which is the filename that we want to create. 
workbook = xlsxwriter.Workbook('carsData1.xlsx') 

# The workbook object is then used to add new 
# worksheet via the add_worksheet() method. 
worksheet = workbook.add_worksheet() 

# Use the worksheet object to write 
# data via the write() method. 
worksheet.write('A1', 'Name') 
worksheet.write('B1', 'Age') 
worksheet.write('C1', 'Subject') 
worksheet.write('D1', 'Country') 

# Finally, close the Excel file 
# via the close() method. 
workbook.close()

 

 

Now if you execute this app.py file you will see the following output excel file will be created

 

 

 

 

 

 

 

Now we will see a second example in which we will be populating values inside different rows

 

See also  Python 3 Script to Print Inverted Pyramid of Numbers Pattern Using For Loop Full Project For Beginners

 

# import xlsxwriter module 
import xlsxwriter 

workbook = xlsxwriter.Workbook('Example2.xlsx') 
worksheet = workbook.add_worksheet() 

# Start from the first cell. 
# Rows and columns are zero indexed. 
row = 0
column = 0

content = ["ankit", "rahul", "priya", "harshita", 
                    "sumit", "neeraj", "shivam"] 

# iterating through content list 
for item in content : 

    # write operation perform 
    worksheet.write(row, column, item) 

    # incrementing the value of row by one 
    # with each iteratons. 
    row += 1
    
workbook.close()

 

 

 

 

 

 

Now we will create another which will fill both rows and columns

 

 

# import xlsxwriter module 
import xlsxwriter 

workbook = xlsxwriter.Workbook('Example3.xlsx') 

# By default worksheet names in the spreadsheet will be 
# Sheet1, Sheet2 etc., but we can also specify a name. 
worksheet = workbook.add_worksheet("My sheet") 

# Some data we want to write to the worksheet. 
scores = ( 
    ['ankit', 1000], 
    ['rahul', 100], 
    ['priya', 300], 
    ['harshita', 50], 
) 

# Start from the first cell. Rows and 
# columns are zero indexed. 
row = 0
col = 0

# Iterate over the data and write it out row by row. 
for name, score in (scores): 
    worksheet.write(row, col, name) 
    worksheet.write(row, col + 1, score) 
    row += 1

workbook.close()

 

See also  Python Tkinter Window or Canvas Resizer to Custom Width and Height GUI Desktop App Full Project For Beginners

 

 

 

 

 

Leave a Reply