|
| 1 | +""" |
| 2 | + In this script we're going to create a simple excel file with 3(Three) columns |
| 3 | + Name | Age | Gender |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +# xlwt module helps make a new excel file |
| 9 | +importxlwt |
| 10 | + |
| 11 | +# create a dictionary with information |
| 12 | +DATA_= { |
| 13 | +'Person1' : { |
| 14 | +'Name' :'Bill Gates', |
| 15 | +'Age' :63, |
| 16 | +'Gender' :'Male' |
| 17 | +}, |
| 18 | +'Person2' : { |
| 19 | +'Name' :'Sheryl Sandberg', |
| 20 | +'Age' :50, |
| 21 | +'Gender' :'Female' |
| 22 | +}, |
| 23 | +'Person3' : { |
| 24 | +'Name' :'Tim Cook', |
| 25 | +'Age' :58, |
| 26 | +'Gender' :'Male' |
| 27 | +}, |
| 28 | +} |
| 29 | + |
| 30 | +# first create a new workbook |
| 31 | +workbook=xlwt.Workbook() |
| 32 | + |
| 33 | +# add a new sheet to the workbook |
| 34 | +sheet=workbook.add_sheet('Sheet1') |
| 35 | + |
| 36 | +# create columns |
| 37 | +sheet.write(0,0,'Name') |
| 38 | +sheet.write(0,1,'Age') |
| 39 | +sheet.write(0,2,'Gender') |
| 40 | + |
| 41 | + |
| 42 | +# we will keep counter running until we loop through all the persons inside DATA_ |
| 43 | +counter=1 |
| 44 | + |
| 45 | +# now lets get the details from the DATA_ dict and insert them into our excel sheet |
| 46 | +fork,vinDATA_.items(): |
| 47 | +sheet.write(counter,0,v['Name']) |
| 48 | +sheet.write(counter,1,v['Age']) |
| 49 | +sheet.write(counter,2,v['Gender']) |
| 50 | + |
| 51 | +# now increment the counter each time |
| 52 | +# this will make sure that information are added on new line for each person |
| 53 | +counter+=1 |
| 54 | + |
| 55 | +# time to save the workbook |
| 56 | +workbook.save('details.xls') |