Python: Write Data directly to Excel

Hi friends, if you are working in IT field, then there must be many times when you have to make report in excel, which must be boring if you have to make that report everyday. Here I am presenting the basic way to write data to excel. 

I am writing numbers 1 to 13 in  first row and value of a large string, separated by ',' in second row. 

You need to install xlwt first. I will prefer below official source:

https://pypi.python.org/pypi/xlwt 

Note:

1. The first line may look like a comment, but without this, this will produce errors in some cases, like when you change the string. This script works with Python 2.x.  

2. What? font.height = 220 !!!

Actually this is how the height is defined:

for height 11, you have to do this :
 11 * 20 = 220 << You have to use this value. To sum up, always multiply with 20 for this value.

------------------------------------------------------------------------------------------------------------------------

#coding: utf8
 

import xlwt
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('sheet 1')

#Create a style for font and its size for first line
 

style = xlwt.XFStyle()
font = xlwt.Font()
font.name = 'Arial'

font.height = 220
 

# Set the style's font to this new one you set up
style.font = font


#split lines
string1 = ' adequation,civil rights,commensurateness,coordination,correspondence,equal,opportunity,equatability,equilibrium,equipoise,equivalence,evenness,fair play'
list1 = string1.decode('utf-8').split(',')
print list1
for i in range(1, 14):
    sheet.write(0,i-1,i, style)
for i in range(1, 14):
    sheet.write(1,i-1,list1[i-1], style)
wbk.save('text11.xls')

Comments