开发阶段数据库总是有变动,开发人员需要维护文档给相关人员使用,故编写一个脚本自动生成数据库文档
生成的excel如下
import cx_Oracle import os from openpyxl import load_workbook from openpyxl import Workbook from openpyxl.styles import PatternFill os.environ[‘NLS_LANG‘] = ‘SIMPLIFIED CHINESE_CHINA.UTF8‘ tableSql = "select * from user_tab_comments" tableColumnSql = "SELECT CC.column_name,CC.data_type,CC.data_length,CC.default_length,CM.comments " "FROM USER_TAB_COLUMNS cc INNER JOIN USER_COL_COMMENTS cm ON cc.column_name = cm.column_name " "WHERE CC.table_name =‘%s‘ AND CM.table_name =‘%s‘ ORDER BY CC.column_id" ‘‘‘ 保存数据字典到指定./space目录下 space->环境(test,uat,pro) usr->用户名 pwd->密码 host->主机名 sid->sid ‘‘‘ def saveDbSchema(space, usr, pwd, host, sid): isExsit = os.path.exists(space) if not isExsit: os.makedirs(space) print("create dir->%s" % (space)) constr = "%s/%[email protected]%s/%s" % (usr, pwd, host, sid) print(constr) conn = cx_Oracle.connect(constr) cursor = conn.cursor() cursor.execute(tableSql) datas = cursor.fetchall() wb = Workbook() # load_workbook("mergeData.xlsx") title = ["字段名", "字段类型", "字段长度", "默认值", "备注"] ws_inx = wb["Sheet"] ws_inx.append(["表名", "备注", "链接"]) ws_inx.column_dimensions[‘A‘].width = 30.0 ws_inx.column_dimensions[‘B‘].width = 25.0 ws_inx.column_dimensions[‘C‘].width = 10.0 for item in datas: sql = tableColumnSql % (item[0], item[0]) cursor.execute(sql) data = cursor.fetchall() ws = wb.create_sheet(item[0]) ws.column_dimensions[‘A‘].width = 20.0 ws.column_dimensions[‘B‘].width = 12.0 ws.column_dimensions[‘C‘].width = 10.0 ws.column_dimensions[‘D‘].width = 20.0 ws.column_dimensions[‘E‘].width = 30.0 ws.append(["表名", item[2], None, "返回"]) ws.cell(1, 4).hyperlink = "#Sheet!A1" ws.cell(1, 4).fill = PatternFill("solid", fgColor="1874CD") ws.append(title) for inx, val in enumerate(data): ws.append([val[0], val[1], val[2], val[3], val[4]]) ws_inx.append([item[0], str(item[2]), "链接"]) ws_inx.cell(ws_inx.max_row, ws_inx.max_column).hyperlink = "#‘%s‘!A1" % (item[0]) wb.save("./%s/%s.xlsx" % (space, usr)) print("ok")
原文地址:https://www.cnblogs.com/lobin/p/10178887.html
时间: 2024-10-08 06:46:35