13
13
from openpyxl .comments import Comment
14
14
from openpyxl .styles import Style , PatternFill , Border , Side , Alignment , Protection , Font , Color
15
15
from openpyxl .styles import colors , borders , fills
16
+ import re
16
17
17
18
18
19
def load_xlsx ():
@@ -80,8 +81,10 @@ def write_only():
80
81
def load_schema (filename ):
81
82
"""先加载schema.sql文件来获取所有建表语句"""
82
83
result = []
84
+ pat = re .compile (r'.* DEFAULT (\S+) .*' )
83
85
with open (filename , encoding = 'utf-8' ) as sqlfile :
84
86
each_table = [] # 每张表定义
87
+ temp_comment = ''
85
88
for line in sqlfile :
86
89
if line .startswith ('--' ):
87
90
temp_comment = line .split ('--' )[1 ].strip ()
@@ -96,9 +99,12 @@ def load_schema(filename):
96
99
col_null = 'NOT NULL'
97
100
else :
98
101
col_null = ''
99
- col_remark = col_arr [- 1 ]
100
- cr = col_remark .replace ("'" , "" )
101
- each_table .append ((col_name , col_type , col_null , cr [:- 1 ] if cr .endswith (',' ) else cr ))
102
+ col_remark = line .split (' COMMENT ' )
103
+ cr = col_remark [- 1 ].strip ().replace ("'" , "" )
104
+ defaultmatch = pat .match (line )
105
+ default = defaultmatch .group (1 ) if defaultmatch else ''
106
+ each_table .append ((col_name , col_type , col_null ,
107
+ default , cr [:- 1 ] if cr .endswith (',' ) else cr ))
102
108
elif 'ENGINE=' in line :
103
109
# 单个表定义结束
104
110
result .append (list (each_table ))
@@ -139,15 +145,17 @@ def write_dest(xlsx_name, schema_name):
139
145
140
146
for table in table_data :
141
147
ws = wb .create_sheet (title = table [0 ])
142
- ws .merge_cells ('E3:H3 ' ) # 合并单元格
148
+ ws .merge_cells ('E3:I3 ' ) # 合并单元格
143
149
ws ['E3' ].style = title_style
144
150
ws ['F2' ].style = Style (border = Border (
145
151
bottom = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
146
152
ws ['G2' ].style = Style (border = Border (
147
153
bottom = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
148
154
ws ['H2' ].style = Style (border = Border (
149
155
bottom = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
150
- ws ['I3' ].style = Style (border = Border (
156
+ ws ['I2' ].style = Style (border = Border (
157
+ bottom = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
158
+ ws ['J3' ].style = Style (border = Border (
151
159
left = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
152
160
ws ['E3' ] = table [0 ]
153
161
ws ['E4' ].style = header_style
@@ -157,11 +165,14 @@ def write_dest(xlsx_name, schema_name):
157
165
ws ['G4' ].style = header_style
158
166
ws ['G4' ] = '空值约束'
159
167
ws ['H4' ].style = header_style
160
- ws ['H4' ] = '备注'
168
+ ws ['H4' ] = '默认值'
169
+ ws ['I4' ].style = header_style
170
+ ws ['I4' ] = '备注'
161
171
ws .column_dimensions ['E' ].width = 20
162
172
ws .column_dimensions ['F' ].width = 20
163
- ws .column_dimensions ['G' ].width = 16
164
- ws .column_dimensions ['H' ].width = 45
173
+ ws .column_dimensions ['G' ].width = 12
174
+ ws .column_dimensions ['H' ].width = 16
175
+ ws .column_dimensions ['I' ].width = 45
165
176
for idx , each_column in enumerate (table [2 :]):
166
177
ws ['E{}' .format (idx + 5 )].style = common_style
167
178
ws ['E{}' .format (idx + 5 )] = each_column [0 ]
@@ -171,6 +182,8 @@ def write_dest(xlsx_name, schema_name):
171
182
ws ['G{}' .format (idx + 5 )] = each_column [2 ]
172
183
ws ['H{}' .format (idx + 5 )].style = common_style
173
184
ws ['H{}' .format (idx + 5 )] = each_column [3 ]
185
+ ws ['I{}' .format (idx + 5 )].style = common_style
186
+ ws ['I{}' .format (idx + 5 )] = each_column [4 ]
174
187
ws = wb ['首页列表' ]
175
188
ws .merge_cells ('D3:F3' )
176
189
ws ['D3' ].style = title_style
@@ -180,7 +193,7 @@ def write_dest(xlsx_name, schema_name):
180
193
bottom = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
181
194
ws ['G3' ].style = Style (border = Border (
182
195
left = Side (border_style = borders .BORDER_THIN , color = 'FF000000' )))
183
- ws ['D3' ] = '贷快发数据库系统表 '
196
+ ws ['D3' ] = 'MySQL数据库系统表 '
184
197
ws ['D4' ].style = header_style
185
198
ws ['D4' ] = '编号'
186
199
ws ['E4' ].style = header_style
@@ -206,7 +219,8 @@ def write_dest(xlsx_name, schema_name):
206
219
# write_xlsx()
207
220
# write_only()
208
221
import sys
209
- # dest_file = r'D:\work\fastloan\trunk\01文档\03系统设计\03数据库设计\贷快发MySQL数据库设计.xlsx'
210
- # sql_file = r'D:\work\fastloan\trunk\fastloan\src\main\resources\sql\schema.sql'
211
- write_dest (sys .argv [1 ], sys .argv [2 ])
222
+ dest_file = r'D:\work\MySQL数据库设计.xlsx'
223
+ schema_file = r'D:\work\projects\gitprojects\tobacco\src\main\resources\sql\schema.sql'
224
+ write_dest (dest_file , schema_file )
225
+ # write_dest(sys.argv[1], sys.argv[2])
212
226
pass
0 commit comments