Write excel from code
petercour
Posted on July 18, 2019
You can write Excel files with Python. Excel is a very popular file format outside of the development community. Finance and many other fields heavily use Excel.
The module xlsxwriter lets you do that, first install that module.
Then load the module in Python.
#!/usr/bin/python3
#coding: utf-8
import xlsxwriter
Create a file and a sheet to work in
file_name = "data.xlsx"
workbook = xlsxwriter.Workbook(file_name)
worksheet = workbook.add_worksheet('sheet1')
Write the header to the excel sheet
worksheet.write(0, 0, 'id')
worksheet.write(0,1, 'name')
worksheet.write(0,2, 'class')
worksheet.write(0,3, 'data')
And finally write the data
worksheet.write_row(1, 0, [1, 2, 3])
worksheet.write_column('D2', ['a', 'b', 'c'])
workbook.close()
Put it all together to write the excel file. It gives you this code:
#!/usr/bin/python3
#coding: utf-8
import xlsxwriter
file_name = "data.xlsx"
workbook = xlsxwriter.Workbook(file_name)
worksheet = workbook.add_worksheet('sheet1')
worksheet.write(0, 0, 'id')
worksheet.write(0,1, 'name')
worksheet.write(0,2, 'class')
worksheet.write(0,3, 'data')
worksheet.write_row(1, 0, [1, 2, 3])
worksheet.write_column('D2', ['a', 'b', 'c'])
workbook.close()
Related links:
đź’– đź’Ş đź™… đźš©
petercour
Posted on July 18, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.