FAQ
Please let me know how can i import my sql data of multiple rows and
columns into an excel sheet.
here i need to adjust the column width based on the on the data that
sits into the column

Search Discussions

  • Martin Brochhaus at Jun 2, 2011 at 12:19 pm
    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
  • Hisan at Jun 2, 2011 at 5:25 pm

    On Jun 2, 5:19?pm, Martin Brochhaus wrote:
    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


    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
  • Hisan at Jun 3, 2011 at 12:22 pm
    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





    On Jun 2, 2:48?pm, Dennis Lee Bieber 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/
  • Kushal Kumaran at Jun 3, 2011 at 12:41 pm

    On Fri, Jun 3, 2011 at 5:52 PM, hisan 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 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
  • Prasad, Ramit at Jun 6, 2011 at 4:35 pm

    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 as to 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 are believed 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 way from 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.
  • Benjamin Kaplan at Jun 6, 2011 at 6:43 pm

    On Mon, Jun 6, 2011 at 9:35 AM, Prasad, Ramit wrote:
    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
    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.
  • Prasad, Ramit at Jun 6, 2011 at 8:18 pm

    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
    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.
    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
    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 as to 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 are believed 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 way from 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.
  • John Nagle at Jun 30, 2011 at 6:00 pm

    On 6/2/2011 5:11 AM, hisan wrote:
    Please let me know how can i import my sql data of multiple rows and
    columns into an excel sheet.
    here i need to adjust the column width based on the on the data that
    sits into the column
    You're asking in the wrong forum. Try the MySQL forum or an
    Excel forum.

    For a one-off job, use the MySQL Workbench, do a SELECT, click on
    the floppy disk icon, and export a CSV (comma-separated value) file,
    which Excel will import.

    It's possible to link Excel directly to an SQL database; see
    the Excel documentation.

    On a server, you can SELECT ... INTO OUTFILE and get a CSV file
    that way, but the file is created on the machine where the database
    is running, not the client machine.

    You can write a Python program to SELECT from the database and
    use the CSV module to create a CSV file, but as a one-off, it's
    not necessary.

    John Nagle

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 2, '11 at 12:11p
activeJun 30, '11 at 6:00p
posts9
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase