怎么在Python中使用win32com模块生成词表格

  介绍

怎么在Python中使用win32com模块生成词表格?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

<强>连接mysql

import  MySQLdb   db_host =,““   db_port =3306   db_name =,““   db_user =,““   db_pwd =,““   db =, MySQLdb.connect(主机=db_host端口=db_port用户=db_user passwd=db_pwd db=db_name charset=皍tf8")   cursor =, db.cursor ()

<强>获取所有表结构

#获取表数据库中所有表和备注   db_name def  get_table(光标):   sql =,才能“select  table_name, table_comment 得到information_schema.tables  where  table_schema =, & # 39;“, +, db_name  +,“& # 39;“   cursor.execute才能(sql)   时间=result 才能;cursor.fetchall ()   tables 才能=,{}   for 才能;r 结果:拷贝   ,,,表(r [0]), r=, [1]   return 才能;表   #获取表结构   db_name def  get_table_desc(光标,table_name):   sql =,才能“select  column_name, column_type, column_default, is_nullable, column_comment 得到information_schema.columns  where  table_schema =, & # 39;“, +, db_name  +,“& # 39;,以及table_name =, & # 39;“, +, table_name  +,“& # 39;“,   cursor.execute才能(sql)   时间=result 才能;cursor.fetchall ()   return 才能;结果

<强> win32com操作

得到win32com.client  import 调度,常量   时间=word 调度(& # 39;Word.Application& # 39;)   时间=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“, #设置字体大小   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"列备注“

<强>完整代码

#编码:utf - 8   #把数据库中的表结构导出到词的表格中,完成设计文档   #不会用win32com操作字样式   import  MySQLdb配置   得到win32com.client  import 调度,常量   时间=db_name “crawlerdb_update"   db =, MySQLdb.connect(主机=config.db_host端口=config.db_port用户=config.db_user passwd=config.db_pwd db=db_name charset=皍tf8")   时间=cursor  db.cursor ()   db_name def  get_table(光标):   sql =,才能“select  table_name, table_comment 得到information_schema.tables  where  table_schema =, & # 39;“, +, db_name  +,“& # 39;“   cursor.execute才能(sql)   时间=result 才能;cursor.fetchall ()   tables 才能=,{}   for 才能;r 结果:拷贝   ,,,表(r [0]), r=, [1]   return 才能;表   db_name def  get_table_desc(光标,table_name):   sql =,才能“select  column_name, column_type, column_default, is_nullable, column_comment 得到information_schema.columns  where  table_schema =, & # 39;“, +, db_name  +,“& # 39;,以及table_name =, & # 39;“, +, table_name  +,“& # 39;“,   cursor.execute才能(sql)   时间=result 才能;cursor.fetchall ()   return 才能结果   时间=tables  get_table(游标,db_name)   时间=word 调度(& # 39;Word.Application& # 39;)   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 列举拷贝(表):   tables_desc 才能=,get_table_desc(光标,db_name table_name)   print  r.Start才能   r.InsertBefore才能(“\ n", +,表(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"是否为空”;   null   null   null   null   null   null   null   null

怎么在Python中使用win32com模块生成词表格