Aw: How to import data from MySQL db into excel sheet

  • Thread starter Martin Brochhaus
  • Start date
M

Martin Brochhaus

Why do you need to do this with python? Why not output the SQL data as a .cvs and open that file in Excel. The user can then adjust column widths as he likes.

If it has to be done programatically, you might want to start your journey here:
http://www.python-excel.org/

Best regards,
Martin
 
H

hisan

Why do you need to do this with python? Why not output the SQL data as a ..cvs and open that file in Excel. The user can then adjust column widths ashe likes.

If it has to be done programatically, you might want to start your journey here:http://www.python-excel.org/

Best regards,
Martin



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
 
D

Dennis Lee Bieber

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...
 
H

hisan

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.

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
 
K

Kushal Kumaran

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.
 
P

Prasad, Ramit

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

If you are using xlwt:
sheet.col(9).width = 3200

I am not sure exactly what unit the 3200 represents so I just adjust this manually to be a size that works for me.


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted asto completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments arebelieved to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any wayfrom its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety,whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
 
B

Benjamin Kaplan

If you are using xlwt:
sheet.col(9).width = 3200

I am not sure exactly what unit the 3200 represents so I just adjust this manually to be a size that works for me.


Ramit

xlwt is a package for editing Excel files. CSV, despite being a format
that Excel can open, is not an Excel file. A CSV is to spreadsheets
what plain text is to word processing. It's an extremely simple, easy
to use format programaticfally but it doesn't support any formattting
of any kind.
 
P

Prasad, Ramit

Currently i am importing the Database into CSV file using csv module,
xlwt is apackage for editing Excel files. CSV, despite being a format
that Excel can open, is not an Excel file. A CSV is to spreadsheets
whatplain text is to word processing. It's an extremely simple, easy
to use format programaticfally but it doesn't support any formattting
of any kind.

Topic says importing into excel, so I assume the OP is *currently* using CSV but wants to *switch* to Excel. If that is true then the following is the syntax for it. Assuming you open a workbook and create a worksheet within with a local name of "sheet".

sheet.col(9).width = 3200


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted asto completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments arebelieved to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any wayfrom its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety,whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top