python 3.7 生成數據庫文檔
阿新 • • 發佈:2018-12-26
有變 save att const 開發 imp lang open space
開發階段數據庫總是有變動,開發人員需要維護文檔給相關人員使用,故編寫一個腳本自動生成數據庫文檔
生成的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/%s@%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")
python 3.7 生成數據庫文檔