python模块win32com 实现数据库表结构自动生成word表格
2016-08-29 13:56
761 查看
下载win32模块
下载链接连接mysql
import MySQLdb db_host = "" db_port = 3306 db_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 result
win32com操作
from win32com.client import Dispatch,constants word = Dispatch('Word.Application') word.Visible = 1 #是否在后台运行word word.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"列备注"
完整代码
github地址相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- 数据库链接字符串查询网站
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定