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