码迷,mamicode.com
首页 > 数据库 > 详细

python 3.7 生成数据库文档

时间:2018-12-26 14:29:06      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:open   主机名   print   merge   not   path   cell   where   cut   

开发阶段数据库总是有变动,开发人员需要维护文档给相关人员使用,故编写一个脚本自动生成数据库文档

生成的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 生成数据库文档

标签:open   主机名   print   merge   not   path   cell   where   cut   

原文地址:https://www.cnblogs.com/lobin/p/10178887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!