Python Imports database content into Excel

Source: Internet
Author: User

Each time the data to the operating guide, if not using tools, is directly generated in the CSV format files, such files do not support the ' sheet ', and each time there is a manual, rather unscientific, try Python to generate Excel files.

It's a bit rough, but it's a good start!

Improved version: Database content to Excel

       #!/usr/bin/env python
 
       import xlwt
       import   mysqldb
       WBK=XLWT. Workbook ()
       sheet=wbk.add_sheet (' Sheet 1 ')
       sheet.write (0,0, ' Company ')
       sheet.write (0,1, ' city ')
       Row=1
       conn=mysqldb.connect (host= ' xxx ', user= ' xxx ', passwd= ' xxx ', db= ' gh ')
       cursor=conn.cursor ()
       Cursor.execute (' select * from customers ') to
       com,city in Cursor.fetchall ():
               sheet.write (row,0,com)
               Sheet.write (row,1,city)
                row+=1
        wbk.save (city.xls
        ) cursor.close () Conn.commit () Conn.close ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.