forked from yidao620c/python3-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_schema.py
More file actions
226 lines (209 loc) · 9.07 KB
/
Copy pathgenerate_schema.py
File metadata and controls
226 lines (209 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 通过一个schema.sql来生成excel表格的数据库设计文档
Desc :
"""
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.compat import range
from openpyxl.cell import get_column_letter
from openpyxl.drawing import Image
from openpyxl.writer.dump_worksheet import WriteOnlyCell
from openpyxl.comments import Comment
from openpyxl.styles import Style, PatternFill, Border, Side, Alignment, Protection, Font, Color
from openpyxl.styles import colors, borders, fills
import re
def load_xlsx():
wb = Workbook()
ws = wb.active
ws.title = "首页列表"
ws = wb['首页列表']
print(wb.get_sheet_names())
print(ws['D5'], ws.cell(row=5, column=4))
cell_range = ws['A1':'C2']
wb2 = load_workbook('D:/work/MySQL数据库表.xlsx')
print(wb2.get_sheet_names())
def write_xlsx():
wb = Workbook()
dest_filename = 'empty_book.xlsx'
ws = wb.active
ws.title = "首页列表"
for col_idx in range(1, 10):
col = get_column_letter(col_idx)
for row in range(1, 20):
ws['%s%s' % (col, row)].value = '%s%s' % (col, row)
ws.merge_cells('A1:B1') # 合并单元格
ws.unmerge_cells('A1:B1')
ws = wb.create_sheet()
ws.title = 'Pi'
ws['F5'] = 3.14
# img = Image('logo.png')
# img.drawing.top = 100
# img.drawing.left = 150
wb.save(filename=dest_filename)
wb = load_workbook(filename='empty_book.xlsx')
sheet_ranges = wb['首页列表']
print(sheet_ranges['D18'].value)
def write_only():
wb = Workbook()
ws = wb.create_sheet()
ws.title = "首页列表"
c = ws['A1']
c.style = Style(font=Font(name='Courrier', size=36)
, fill=PatternFill(fill_type=None, start_color='FFFFFFFF',
end_color='FF000000')
, protection=Protection(locked='inherit', hidden='inherit')
, alignment=Alignment(horizontal='general', vertical='bottom',
shrink_to_fit=True)
, border=Border(left=Side(border_style=None, color='FF000000')))
c.value = '姓名'
# cell = WriteOnlyCell(ws, value="hello world")
# cell.style = Style(font=Font(name='Courrier', size=36))
# cell.comment = Comment(text="A comment", author="Author's Name")
# ws.header_footer.center_header.text = 'My Excel Page'
# ws.header_footer.center_header.font_size = 14
# ws.header_footer.center_header.font_name = "Tahoma,Bold"
# ws.header_footer.center_header.font_color = "CC3366"
wb.save(filename='empty_book.xlsx')
def load_schema(filename):
"""先加载schema.sql文件来获取所有建表语句"""
result = []
pat = re.compile(r'.* DEFAULT (\S+) .*')
with open(filename, encoding='utf-8') as sqlfile:
each_table = [] # 每张表定义
temp_comment = ''
for line in sqlfile:
if line.startswith('--'):
temp_comment = line.split('--')[1].strip()
elif 'DROP TABLE' in line:
each_table.insert(0, temp_comment)
each_table.insert(1, line.strip().split()[-1][:-1])
elif ' COMMENT ' in line and 'ENGINE=' not in line:
col_arr = line.split()
col_name = col_arr[0]
col_type = col_arr[1]
if 'PRIMARY KEY' in line or 'NOT NULL' in line:
col_null = 'NOT NULL'
else:
col_null = ''
col_remark = line.split(' COMMENT ')
cr = col_remark[-1].strip().replace("'", "")
defaultmatch = pat.match(line)
default = defaultmatch.group(1) if defaultmatch else ''
each_table.append((col_name, col_type, col_null,
default, cr[:-1] if cr.endswith(',') else cr))
elif 'ENGINE=' in line:
# 单个表定义结束
result.append(list(each_table))
each_table.clear()
return result
def write_dest(xlsx_name, schema_name):
border = Border(
left=Side(border_style=borders.BORDER_THIN, color='FF000000'),
right=Side(border_style=borders.BORDER_THIN, color='FF000000'),
top=Side(border_style=borders.BORDER_THIN, color='FF000000'),
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')
)
alignment = Alignment(horizontal='justify', vertical='bottom',
text_rotation=0, wrap_text=False,
shrink_to_fit=True, indent=0)
fill = PatternFill(fill_type=None, start_color='FFFFFFFF')
# 基本的样式
basic_style = Style(font=Font(name='Microsoft YaHei')
, border=border, alignment=alignment
, fill=fill)
title_style = basic_style.copy(
font=Font(name='Microsoft YaHei', b=True, size=20, color='00215757'),
alignment=Alignment(horizontal='center', vertical='bottom',
text_rotation=0, wrap_text=False,
shrink_to_fit=True, indent=0),
fill=PatternFill(fill_type=fills.FILL_SOLID, start_color='00B2CBED'))
header_style = basic_style.copy(
font=Font(name='Microsoft YaHei', b=True, size=15, color='00215757'),
fill=PatternFill(fill_type=fills.FILL_SOLID, start_color='00BAA87F'))
common_style = basic_style.copy()
link_style = basic_style.copy(font=Font(
name='Microsoft YaHei', color=colors.BLUE, underline='single'))
table_data = load_schema(schema_name)
wb = Workbook()
wb.active.title = "首页列表"
for table in table_data:
ws = wb.create_sheet(title=table[0])
ws.merge_cells('E3:I3') # 合并单元格
ws['E3'].style = title_style
ws['F2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['G2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['H2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['I2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['J3'].style = Style(border=Border(
left=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['E3'] = table[0]
ws['E4'].style = header_style
ws['E4'] = '列名'
ws['F4'].style = header_style
ws['F4'] = '类型'
ws['G4'].style = header_style
ws['G4'] = '空值约束'
ws['H4'].style = header_style
ws['H4'] = '默认值'
ws['I4'].style = header_style
ws['I4'] = '备注'
ws.column_dimensions['E'].width = 20
ws.column_dimensions['F'].width = 20
ws.column_dimensions['G'].width = 12
ws.column_dimensions['H'].width = 16
ws.column_dimensions['I'].width = 45
for idx, each_column in enumerate(table[2:]):
ws['E{}'.format(idx + 5)].style = common_style
ws['E{}'.format(idx + 5)] = each_column[0]
ws['F{}'.format(idx + 5)].style = common_style
ws['F{}'.format(idx + 5)] = each_column[1]
ws['G{}'.format(idx + 5)].style = common_style
ws['G{}'.format(idx + 5)] = each_column[2]
ws['H{}'.format(idx + 5)].style = common_style
ws['H{}'.format(idx + 5)] = each_column[3]
ws['I{}'.format(idx + 5)].style = common_style
ws['I{}'.format(idx + 5)] = each_column[4]
ws = wb['首页列表']
ws.merge_cells('D3:F3')
ws['D3'].style = title_style
ws['E2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['F2'].style = Style(border=Border(
bottom=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['G3'].style = Style(border=Border(
left=Side(border_style=borders.BORDER_THIN, color='FF000000')))
ws['D3'] = 'MySQL数据库系统表'
ws['D4'].style = header_style
ws['D4'] = '编号'
ws['E4'].style = header_style
ws['E4'] = '表名'
ws['F4'].style = header_style
ws['F4'] = '详情链接'
ws.column_dimensions['D'].width = 15
ws.column_dimensions['E'].width = 25
ws.column_dimensions['F'].width = 35
for inx, val in enumerate(table_data):
ws['D{}'.format(inx + 5)].style = common_style
ws['D{}'.format(inx + 5)] = inx + 1
ws['E{}'.format(inx + 5)].style = common_style
ws['E{}'.format(inx + 5)] = val[1]
linkcell = ws['F{}'.format(inx + 5)]
linkcell.style = link_style
linkcell.value = val[0]
linkcell.hyperlink = '#{0}!{1}'.format(val[0], 'E3')
wb.save(filename=xlsx_name)
if __name__ == '__main__':
# write_xlsx()
# write_only()
import sys
dest_file = r'D:\work\MySQL数据库设计.xlsx'
schema_file = r'D:\work\projects\gitprojects\tobacco\src\main\resources\sql\schema.sql'
write_dest(dest_file, schema_file)
# write_dest(sys.argv[1], sys.argv[2])
pass