How to import data from MySQL db into excel sheet -- Very Urgent

Kushal Kumaran kushal.kumaran+python at gmail.com
Fri Jun 3 08:41:21 EDT 2011


On Fri, Jun 3, 2011 at 5:52 PM, hisan <santosh.ssit at gmail.com> wrote:
> Task i need to achieve here is:
> I need to write a python script which fetches all the data from the
> MySQL database and dumps into an excel sheet. since i was not able to
> dump the data into excel sheet i used CSV file which looks similar to
> excel sheet.
> on dumping the data into CSV file i came know that column width is too
> small and hence it was not possible to display data properly.
> Hence i requested the way to increase the column widht to accommodate
> data of any length.
>

The xlwt module lets you write excel files from python.  There is more
information at http://www.python-excel.org/.  The examples page has an
example called col_width.py, which should be what you need.

> below is my sample code
>
>
> import os,time
> import MySQLdb
> import csv
> db=MySQLdb.Connect("localhost","root","san123","phone")
> cursor=db.cursor()
>
> cursor.execute("select column_name from information_schema.columns
> where table_name='phonebook'")
> row=cursor.fetchall()
> f=open(os.getcwd()+"\\analytics.csv",'w')
> writer = csv.writer(f)
> writer.writerow(row)
> cursor.execute("select * from phonebook")
> col_val=cursor.fetchall()
> writer.writerows(col_val)
> f.close()
>
> My Main objective is to fetch the data and from database and dump it
> into excel sheet or csv file using Python.
> If there any ways to achieve this using python please let me know.
>
> Waiting for the early response-
>
> I need to achieve my task using python only
>
>
>
>
>
>
> On Jun 2, 2:48 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>> On Thu, 2 Jun 2011 10:25:24 -0700 (PDT), hisan <santosh.s... at gmail.com>
>> declaimed the following in gmane.comp.python.general:
>>
>>
>>
>> > Currently i am importing the Database into CSV file using csv module,
>> > in csv file i need to change the column width according the size of
>> > the data. i need to set different column width for different columns
>> > pleas let me know how to achieve this
>>
>>         Since CSV files are purely text, no Excel Spreadsheet visual
>> attributes can be defined in them...
>>
>>         Take Python out of the equation.
>>
>>         How would you change column width using an Excel VBA script/macro?
>>
>>         When you can answer that, you will have the answer of how to do it
>> from Python...
>> --
>>         Wulfraed                 Dennis Lee Bieber         AF6VN
>>         wlfr... at ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
regards,
kushal



More information about the Python-list mailing list