Export Data from Mysql server to excel file using python:
I have data on mysql server as following
Now i want these data to export to a excel file like bellow .
Just follow as bellow .
Main Python Script:
import mysql.connector
from xlwt import Workbook
con=mysql.connector.connect(host='xxx',database='xxx',user='xxx',password='xxxx')
cur=con.cursor()
wb=Workbook()
sheet1=wb.add_sheet('sheet1')
sql="""select * from employee """
cur.execute(sql)
result=cur.fetchall()
sheet1.write(0,0,"First Name")
sheet1.write(0,1,"Last Name")
sheet1.write(0,2,"Age")
sheet1.write(0,3,"Gender")
sheet1.write(0,4,"Income")
row_number=1
for row in result:
column_number=0
for item in row:
sheet1.write(row_number,column_number,str(item))
column_number=column_number+1
row_number=row_number+1
wb.save('test1.xls')
Thats it . Just fill the following part by yourself .
host='xxx',database='xxx',user='xxx',password='xxxx'
I have data on mysql server as following
Now i want these data to export to a excel file like bellow .
Just follow as bellow .
Main Python Script:
import mysql.connector
from xlwt import Workbook
con=mysql.connector.connect(host='xxx',database='xxx',user='xxx',password='xxxx')
cur=con.cursor()
wb=Workbook()
sheet1=wb.add_sheet('sheet1')
sql="""select * from employee """
cur.execute(sql)
result=cur.fetchall()
sheet1.write(0,0,"First Name")
sheet1.write(0,1,"Last Name")
sheet1.write(0,2,"Age")
sheet1.write(0,3,"Gender")
sheet1.write(0,4,"Income")
row_number=1
for row in result:
column_number=0
for item in row:
sheet1.write(row_number,column_number,str(item))
column_number=column_number+1
row_number=row_number+1
wb.save('test1.xls')
Thats it . Just fill the following part by yourself .
host='xxx',database='xxx',user='xxx',password='xxxx'
No comments:
Post a Comment