时间:2021-05-22
本文实例讲述了Python使用win32com模块实现数据库表结构自动生成word表格的方法。分享给大家供大家参考,具体如下:
下载win32模块
下载链接:https://sourceforge.net/projects/pywin32/files/pywin32/
连接mysql
import MySQLdbdb_host = ""db_port = 3306db_name = ""db_user = ""db_pwd = ""db = MySQLdb.connect(host=db_host,port=db_port,user=db_user,passwd=db_pwd,db=db_name,charset="utf8")cursor = db.cursor()获取所有表结构
#获取表数据库中所有表和备注def get_tables(cursor,db_name): sql = "select table_name,table_comment from information_schema.tables where table_schema = '" + db_name + "'" cursor.execute(sql) result = cursor.fetchall() tables = {} for r in result: tables[r[0]] = r[1] return tables#获取表结构def get_table_desc(cursor,db_name,table_name): sql = "select column_name,column_type,column_default,is_nullable,column_comment from information_schema.columns where table_schema = '" + db_name + "' and table_name = '" + table_name + "'" cursor.execute(sql) result = cursor.fetchall() return resultwin32com操作
from win32com.client import Dispatch,constantsword = Dispatch('Word.Application')word.Visible = 1 #是否在后台运行wordword.DisplayAlerts = 0 #是否显示警告信息doc = word.Documents.Add() #新增一个文档r = doc.Range(0,0) #获取一个范围r.Style.Font.Name = u"Verdana" #设置字体r.Style.Font.Size = "9" #设置字体大小r.InsertBefore("\n" + 表描述 + " " + 表名) #在这个范围前插入文本table = r.Tables.Add(doc.Range(r.End,r.End),字段数+1,5) #建一张表格table.Rows[0].Cells[0].Range.Text = u"列"table.Rows[0].Cells[1].Range.Text = u"类型"table.Rows[0].Cells[2].Range.Text = u"默认值"table.Rows[0].Cells[3].Range.Text = u"是否为空"table.Rows[0].Cells[4].Range.Text = u"列备注"完整代码
#coding:utf-8#把数据库中的表结构导出到word的表格中,完成设计文档#不会用win32com操作word样式import MySQLdb,configfrom win32com.client import Dispatch,constantsdb_name = "crawlerdb_update"db = MySQLdb.connect(host=config.db_host,port=config.db_port,user=config.db_user,passwd=config.db_pwd,db=db_name,charset="utf8")cursor = db.cursor()def get_tables(cursor,db_name): sql = "select table_name,table_comment from information_schema.tables where table_schema = '" + db_name + "'" cursor.execute(sql) result = cursor.fetchall() tables = {} for r in result: tables[r[0]] = r[1] return tablesdef get_table_desc(cursor,db_name,table_name): sql = "select column_name,column_type,column_default,is_nullable,column_comment from information_schema.columns where table_schema = '" + db_name + "' and table_name = '" + table_name + "'" cursor.execute(sql) result = cursor.fetchall() return resulttables = get_tables(cursor,db_name)word = Dispatch('Word.Application')word.Visible = 1 word.DisplayAlerts = 0 doc = word.Documents.Add()r = doc.Range(0,0)r.Style.Font.Name = u"Verdana"r.Style.Font.Size = "9"for k,table_name in enumerate(tables): tables_desc = get_table_desc(cursor,db_name,table_name) print r.Start r.InsertBefore("\n" + tables[table_name] + " " + table_name) table = r.Tables.Add(doc.Range(r.End,r.End),len(tables_desc) + 1,5) table.Rows[0].Cells[0].Range.Text = u"列" table.Rows[0].Cells[1].Range.Text = u"类型" table.Rows[0].Cells[2].Range.Text = u"默认值" table.Rows[0].Cells[3].Range.Text = u"是否为空" table.Rows[0].Cells[4].Range.Text = u"列备注" for i,column in enumerate(tables_desc): for j,col in enumerate(column): if col == None: col = "(NULL)" table.Rows[i+1].Cells[j].Range.Text = col r = doc.Range(table.Range.End,table.Range.End) break更多关于Python相关内容感兴趣的读者可查看本站专题:《Python+MySQL数据库程序设计入门教程》、《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
使用win32com模块开发windowActiveX的示例:(如果你还没有装win32com模块的话,请到http://python.net/crew/ski
环境:Python3.6本文主要是通过win32com操作word,对word中进行常用的操作。本文以替换为例,讲解一下如何使用Python在word中使用“通
本文实例讲述了Python使用pyodbc访问数据库操作方法。数据库连接数据库连接网上大致有两种方法,一种是使用pyodbc,另一种是使用win32com.cl
本文实例讲述了Python使用win32COM实现Excel的写入与保存功能。分享给大家供大家参考,具体如下:很久之前通过东拼西凑实现过使用Python通过wi
DDBuildTools1.1.0是supesoft.com为Asp.Net权限管理系统开发的一套代码生成工具。其主要功能:1.根据数据库表结构生成数据库表结构