借助python工具从word文件中抽取相关表的定义,最后组装建表语句-非常好

借助python工具从word文件中抽取表的定义,最后组装建表语句-非常好

--如有转载请以超链接的方式注明原文章出处,谢谢大家。请尊重每一位乐于分享的原创者

1.python脚本

#
# -*- coding:utf-8 -*-
import sys
from docx import Document

file_path = sys.argv[1]

document = Document(file_path)

tables_info = {}

for table in document.tables:
    rows = table.rows
    for index, row in enumerate(rows):
        if index == 0:
            table_name = row.cells[0].text
            tables_info[table_name] = {}
        elif index == 1:
            continue
        else:
            row_name = row.cells[1].text
            row_type = row.cells[2].text
            tables_info[table_name][row_name] = row_type


for t_name, info in tables_info.items():
    create_table_sql = "create table {t_name}(".format(t_name=t_name)
    for name, _type in info.items():
        if name and _type:
            create_table_sql += '{} {},'.format(name, _type)
    create_table_sql = create_table_sql[:-1] + ');'
    print create_table_sql

2.document文件样本


DJ_YH_FX

字段中文名

字段英文名

类型长度

主键

外键

非空

索引

说明

识别号

sbh

VARCHAR2(20)

旧号

oldh

VARCHAR2(40)

名称

mc

VARCHAR2(80)

代码

dm

VARCHAR2(11)

用户编码

yhbm

VARCHAR2(20)

3. 执行python操作

python parser_docx.py document.docx > create_table.sql

原文地址:https://www.cnblogs.com/iyoume2008/p/8392620.html