您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
怎么在Python中使用win32com模块生成word表格?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
连接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中使用win32com模块生成word表格的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。