Python使用win32com模块实现数据库表结构自动生成word表格的方法

yipeiwu_com6年前Python基础

本文实例讲述了Python使用win32com模块实现数据库表结构自动生成word表格的方法。分享给大家供大家参考,具体如下:

下载win32模块

下载链接:https://sourceforge.net/projects/pywin32/files/pywin32/

连接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"列备注"

完整代码

#coding:utf-8
#把数据库中的表结构导出到word的表格中,完成设计文档
#不会用win32com操作word样式
import MySQLdb,config
from win32com.client import Dispatch,constants
db_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 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
tables = 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程序设计有所帮助。

相关文章

Python性能优化的20条建议

优化算法时间复杂度 算法的时间复杂度对程序的执行效率影响最大,在Python中可以通过选择合适的数据结构来优化时间复杂度,如list和set查找某一个元素的时间复杂度分别是O(n)和O(...

Python减少循环层次和缩进的技巧分析

Python减少循环层次和缩进的技巧分析

本文实例分析了Python减少循环层次和缩进的技巧。分享给大家供大家参考,具体如下: 我们知道Python中冒号和缩进代表大括号,这样写已经可以节省很多代码行数,但是可以更优化,尽可能减...

python hough变换检测直线的实现方法

python hough变换检测直线的实现方法

1 原理  2 检测步骤 将参数空间(ρ,θ) 量化成m*n(m为ρ的等份数,n为θ的等份数)个单元,并设置累加器矩阵,初始值为0; 对图像边界上的每一个点(x,y)带入ρ=...

Python中字典的浅拷贝与深拷贝用法实例分析

Python中字典的浅拷贝与深拷贝用法实例分析

本文实例讲述了Python中字典的浅拷贝与深拷贝用法。分享给大家供大家参考,具体如下: 最近发现的一个很值得记录的东西就是python字典的浅拷贝问题 首先,明确一下什么是浅拷贝,什么是...

Python2.7下安装Scrapy框架步骤教程

Python2.7下安装Scrapy框架步骤教程

由于毕业设计的要求,需要在网站上抓取大量的数据,那么使用Scrapy框架可以让这一过程变得简单不少,毕竟Scrapy是一个为了爬去网站数据、提取结构性数据而编写的应用框架。于是,便开始了...