Examples of how to convert SQLite into an Excel (XLS) table in Python

Source: Internet
Author: User
Tags sqlite database
This article mainly describes the Python implementation of the SQLite database exported to Excel (XLS) Table method, in conjunction with the instance form of Python for the SQLite database connection, read and use the Write operation package (XLWT) to generate Excel table related implementation skills, A friend you need can refer to the following

This example describes how Python implements the process of exporting a SQLite database to an Excel (XLS) table. Share to everyone for your reference, as follows:

1. Assume that the Python environment with the Sliqte library is already installed

Mine is Python2.5.

2. Download the python XLS write Action Pack (XLWT) and install

3. Here is the code (db2xls.py):

Import sqlite3 as Sqlitefrom xlwt import * #MASTER_COLS = [' rowID ', ' type ', ' name ', ' tbl_name ', ' rootpage ', ' SQL ']def Sqlite_ Get_col_names (cur, table): query = ' SELECT ' from%s '% table Cur.execute (query) return [tuple[0] for tuple in Cur.desc Ription]def sqlite_query (cur, table, col = ' * ', where = '): if where! = ': query = ' Select%s from%s where%s '% (c OL, table, where) else:query = ' Select%s ' from%s '% (col, table) cur.execute (query) return Cur.fetchall () def sqli  Te_to_workbook (cur, table, workbook): ws = Workbook.add_sheet (table) print ' CREATE table%s. '% table for Colx, heading In Enumerate (sqlite_get_col_names (cur, table)): Ws.write (0,colx, heading) for Rowy,row in Enumerate (Sqlite_query (CU R, table)): For Colx, text in Enumerate (row): Ws.write (rowy+ 1, Colx, text) def main (dbpath): Xlspath = Dbpath[0:d Bpath.rfind ('. ')] + '. xls ' print ' <%s>-<%s> '% (DBPath, xlspath) db = Sqlite.connect (dbpath) cur = db.cursor () w = Wor Kbook ()  For tbl_name in [row[0] for row in sqlite_query (cur, ' sqlite_master ', ' tbl_name ', ' type = \ ' Table\ ')]: Sqlite_to_wor Kbook (Cur,tbl_name, W) cur.close () db.close () if Tbl_name!=[]: W.save (xlspath) If name = = "main": # arg = = Database PA Th main (sys.argv[1])

4. Usage:

> Python  <path>/db2xls.py  dbpath

If yes, an XLS file with the same name will be generated in the database directory

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.