Using Python to export a database as an Excel table with one click

Source: Internet
Author: User
Exporting database data to an excel table is also a common function. After all, not anyone knows database operation statements. Let's take a look at the effect. The export result of the data source depends on Python. Therefore, you must have Python Environment Support for Python2.7.11. my Python environment is 2.7.11. Although you may use version 3.5, the idea is consistent. Exporting data from the xlwt database to an excel table is also a common function. After all, not anyone knows database operation statements.
Let's take a look at the effect.
Data source
The following is a small case.

# Coding: utf8import sysreload (sys) sys. setdefaultencoding ('utf8 ') # _ author _ = 'Guo Pu' # _ date _ = '2017/20' # _ Desc _ = export data from the database to the excel data table import xlwtimport MySQLdbconn = MySQLdb. connect ('localhost', 'root', 'mysql', 'test', charset = 'utf8') cursor = conn. cursor () count = cursor.exe cute ('select * from message') print count # reset the cursor position cursor. scroll (0, mode = 'absolute ') # Search All results = cursor. fetchall () # obtain the data field name fields = cursor in MYSQL. descriptionworkbook = xlwt. workbook () sheet = workbook. add_sheet ('Table _ message', cell_overwrite_ OK = True) # write the field information for field in range (0, len (fields): sheet. write (0, field, fields [field] [0]) # obtain and write data segment information row = 1col = 0for row in range (1, len (results) + 1 ): for col in range (0, len (fields): sheet. write (row, col, u '% s' % results [row-1] [col]) workbook. save (r '. /readout.xlsx ')


Encapsulation
For ease of use, it is encapsulated into a function that is easy to call.
After encapsulation

# Coding: utf8import sysreload (sys) sys. setdefaultencoding ('utf8 ') # _ author _ = 'Guo Pu' # _ date _ = '2017/20' # _ Desc _ = export data from the database to the excel data table import xlwtimport MySQLdbdef export (host, user, password, dbname, table_name, outputpath): conn = MySQLdb. connect (host, user, password, dbname, charset = 'utf8') cursor = conn. cursor () count = cursor.exe cute ('select * from' + table_name) print count # reset the cursor position cursor. scroll (0, mode = 'absolute ') # Search All results = cursor. fetchall () # obtain the data field name fields = cursor in MYSQL. description workbook = xlwt. workbook () sheet = workbook. add_sheet ('Table _ '+ table_name, cell_overwrite_ OK = True) # write the field information for field in range (0, len (fields): sheet. write (0, field, fields [field] [0]) # obtain and write data segment information row = 1 col = 0 for row in range (1, len (results) + 1): for col in range (0, len (fields): sheet. write (row, col, u '% s' % results [row-1] [col]) workbook. save (outputpath) # Test result if _ name _ = "_ main _": export('localhost', 'root', 'mysql', 'test', 'datetest', r'datetest.xlsx ')


Test results

Id name date1 dlut 2016-07-062 Tsinghua University 2016-07-033 Peking University 2016-07-284 Mark 2016-08-205 Tom 2016-08-196 Jane


The above is a detailed description of how to use Python to export a database as an Excel table with one click. For more information, see other related articles in the first PHP community!

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.