Question

......
......
ofile  = open('test.csv', "wb")
writer = csv.writer(ofile)
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from emp")
mysel=c.execute("select * from emp")
for row in mysel:
    print row
    writer.writerow(row)
ofile.close()
.....
.....

above code is working fine and i am getting results as csv.

Problem starts when i want to write data in csv in mulitple tabs. i checked on this site and people are saying there is no option. i have to use Excel.

to get data populated in different tabs, am trying the below code with

import xlsxwriter
ofile = xlsxwriter.Workbook('mac.xlsx')
worksheet1 = ofile.add_worksheet()
conn=sqlite3.connect('test.sqlite')
c=conn.cursor()
c.execute("select * from emp")
mysel=c.execute("select * from emp ")
for row in mysel:
    print row
    worksheet1.write("A1",row)
    #worksheet1.write(" ",row)
    #worksheet1.write(row)
ofile.close()

no success, please help me to sort the issue, i hope my goal (write data in to excel from sqlite3 using python in multiple tabs) is clear. please share your experienced thoughts

1) can i do this using xlsxwriter 2) should i use any other library 3) is there any simple way to do it

please help

Was it helpful?

Solution

Here's an example how to create an excel file with multiple sheets using xlsxwriter:

from xlsxwriter.workbook import Workbook


workbook = Workbook('output.xlsx')
for i in range(10):
    worksheet = workbook.add_worksheet()
    worksheet.write(0, 0, "I'm sheet number %d" % (i + 1))

workbook.close()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top