Skip to content

Commit 67fee5e

Browse files
authored
Merge pull request #8 from Alinvor/developer
[DONE]合并分支
2 parents 6e2283e + b2d23ef commit 67fee5e

45 files changed

Lines changed: 1417 additions & 35 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ out/
119119
mock/
120120
venv/
121121
venv2/
122+
conf
122123
**/.DS_Store
123124

124125
# Local configuration file

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"editor.formatOnType": true,
99
"editor.wordWrapColumn": 250,
1010
"editor.columnSelection": false,
11-
"editor.mouseWheelZoom": true,
11+
"editor.mouseWheelZoom": false,
1212
"explorer.confirmDelete": false,
1313
"editor.renderWhitespace": "all",
1414
"editor.renderControlCharacters": true,
@@ -30,6 +30,9 @@
3030
},
3131
"markdown.extension.katex.macros": {},
3232
"markdown.extension.toc.levels": "2..6",
33+
"markdown-pdf.highlightStyle": "github.css",
34+
// "markdown-pdf.headerTemplate":"<div style=\"font-size: 9px; margin-left: 1cm;\"> <span class='title'></span></div> <div style=\"font-size: 9px; margin-left: auto; margin-right: 1cm; \"> <span class='date'></span></div>",
35+
"markdown-pdf.headerTemplate": "<div></div>",
3336
"terminal.integrated.copyOnSelection": true,
3437
// "files.autoSave": "afterDelay",
3538
"files.insertFinalNewline": true,

case/excel/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding:utf-8 -*-

case/excel/test_rw_excel.py

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from com.dvsnier.dir.common_dir import generate_complex_or_fmt_file_name
4+
from copy import copy
5+
import datetime
6+
# import os
7+
from openpyxl import Workbook
8+
# from openpyxl.styles import Alignment, Border, Font, PatternFill, Protection, Side, colors
9+
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side, colors
10+
from openpyxl.styles.fills import GradientFill
11+
from openpyxl.styles.named_styles import NamedStyle
12+
from openpyxl.reader.excel import load_workbook
13+
14+
15+
def ready_to_excel(file_name):
16+
'the read data from excel file'
17+
if file_name is None or len(file_name.strip()) == 0:
18+
raise ValueError('the current file name is invaild.')
19+
wb = load_workbook(file_name)
20+
sheet_names = wb.sheetnames
21+
# names = []
22+
for name in sheet_names:
23+
print(name.encode('utf-8'))
24+
# names.append(name.encode('utf-8'))
25+
# print(names)
26+
27+
28+
def write_to_excel(file_name):
29+
'the write data to excel file'
30+
if file_name is None or len(file_name.strip()) == 0:
31+
raise ValueError('the current file name is invaild.')
32+
33+
wb = Workbook() # 工作簿
34+
work_sheet_of_default = wb.active # 当前工作表,等价于wb.active(0)
35+
work_sheet_of_default.title = u"测试0" # 当前工作表title
36+
# 单元格赋值
37+
work_sheet_of_default['A8'] = 'A8'
38+
ws_cell = work_sheet_of_default.cell(row=9, column=1, value='A9')
39+
ws_cell.value = 'A99'
40+
# 合并/撤销单元格
41+
work_sheet_of_default.merge_cells('B2:C2')
42+
# work_sheet_of_default.merge_cells(start_row=2,
43+
# start_column=2,
44+
# end_row=2,
45+
# end_column=3)
46+
work_sheet_of_default['B2'] = u'合并的单元格'
47+
# work_sheet_of_default.unmerge_cells('B2:C2')
48+
# 区间访问
49+
for row in range(1, 10):
50+
work_sheet_of_default.append(range(10))
51+
52+
# 创建新的工作表 WorkSheet
53+
work_sheet_of_test = wb.create_sheet()
54+
# work_sheet_of_test = wb.create_sheet(title='test',
55+
# index=1) # title 和index 可不写省略
56+
work_sheet_of_test.title = u'test1'
57+
work_sheet_of_test.sheet_properties.tabColor = "1072BA" # 设置选项卡背景色
58+
# 单元格取值
59+
# for row in work_sheet_of_test.values():
60+
# for value in row:
61+
# print(value)
62+
# 行列访问
63+
row_3 = work_sheet_of_test['3']
64+
row_range = work_sheet_of_test['3:5']
65+
row_str = 'row: %s\t\t%s' % (row_3, row_range)
66+
row_str = 'row pass'
67+
print(row_str)
68+
column_c = work_sheet_of_test['C']
69+
column_ragne = work_sheet_of_test['C:D']
70+
column_str = 'column: %s\t\t%s' % (column_c, column_ragne)
71+
column_str = 'column pass'
72+
print(column_str)
73+
# 迭代访问,迭代行
74+
for row in work_sheet_of_test.iter_rows(min_row=1, max_row=2, max_col=3):
75+
for cell in row:
76+
# print(cell)
77+
pass
78+
# 迭代访问,迭代列
79+
for col in work_sheet_of_test.iter_cols(min_row=1, max_row=2,
80+
max_col=3): # 只读模式不可用
81+
for cell in col:
82+
# print (cell)
83+
pass
84+
85+
work_sheet_pi = wb.create_sheet(title=u"Pi")
86+
work_sheet_pi['F5'] = 3.14
87+
# 日期格式
88+
work_sheet_pi['A3'] = datetime.datetime.now()
89+
print(work_sheet_pi['A3'].number_format)
90+
# 公式
91+
work_sheet_pi['B2'] = "=SUM(1, 1)"
92+
# 折叠行/列
93+
work_sheet_pi.row_dimensions.group(10, 12, hidden=True)
94+
work_sheet_pi.column_dimensions.group('F', 'G', hidden=True)
95+
96+
work_sheet_ascii = wb.create_sheet(title=u"字母")
97+
for row in range(10, 20):
98+
for col in range(10, 20):
99+
work_sheet_ascii.cell(column=col,
100+
row=row,
101+
value=str("%s%s" % (row, col)))
102+
# minimum_bounding_range = work_sheet_ascii.calculate_dimension()
103+
# print(minimum_bounding_range)
104+
work_sheet_style = wb.create_sheet(title=u"style")
105+
#
106+
# 样式默认值
107+
#
108+
font = Font(name='Calibri',
109+
size=11,
110+
bold=False,
111+
italic=False,
112+
vertAlign=None,
113+
underline='none',
114+
strike=False,
115+
color='FF000000')
116+
fill = PatternFill(fill_type=None,
117+
start_color='FFFFFFFF',
118+
end_color='FF000000')
119+
# border = Border(left=Side(border_style=None, color='FF000000'),
120+
# right=Side(border_style=None, color='FF000000'),
121+
# top=Side(border_style=None, color='FF000000'),
122+
# bottom=Side(border_style=None, color='FF000000'),
123+
# diagonal=Side(border_style=None, color='FF000000'),
124+
# diagonal_direction=0,
125+
# outline=Side(border_style=None, color='FF000000'),
126+
# vertical=Side(border_style=None, color='FF000000'),
127+
# horizontal=Side(border_style=None, color='FF000000'))
128+
# alignment = Alignment(horizontal='general',
129+
# vertical='bottom',
130+
# text_rotation=0,
131+
# wrap_text=False,
132+
# shrink_to_fit=False,
133+
# indent=0)
134+
# number_format = 'General'
135+
# protection = Protection(locked=True, hidden=False)
136+
#
137+
# 单元格样式在对象之间共享,并且一旦分配它们便无法更改
138+
#
139+
# https://openpyxl.readthedocs.io/en/2.6/styles.html#cell-styles-and-named-styles
140+
ws_b5 = work_sheet_style['B5']
141+
font1 = copy(font) # 样式复制
142+
font1.name = 'Arial'
143+
ws_b5.font = font1
144+
ws_b5.value = 'ws_b5'
145+
146+
ws_d6 = work_sheet_style['D6']
147+
font2 = Font(name='Tahoma', color=colors.RED, italic=True, size=16)
148+
ws_d6.font = font2
149+
ws_d6.value = 'ws_d6'
150+
#
151+
# 样式合并单元格
152+
#
153+
work_sheet_style.merge_cells('B8:F10')
154+
top_left_cell = work_sheet_style['B8']
155+
top_left_cell.value = u"样式合并单元格"
156+
# 边
157+
thin = Side(border_style="thin", color="000000")
158+
double = Side(border_style="double", color="ff0000")
159+
# 边框
160+
top_left_cell.border = Border(top=double,
161+
left=thin,
162+
right=thin,
163+
bottom=double)
164+
# 填充
165+
top_left_cell.fill = PatternFill("solid", fgColor="DDDDDD")
166+
fill = GradientFill(stop=("000000", "FFFFFF"))
167+
top_left_cell.fill = fill
168+
top_left_cell.font = Font(b=True, color="FF0000")
169+
# 对齐模式
170+
top_left_cell.alignment = Alignment(horizontal="center", vertical="center")
171+
172+
#
173+
# 页面设置
174+
#
175+
work_sheet_style.page_setup.orientation = work_sheet_style.ORIENTATION_LANDSCAPE
176+
work_sheet_style.page_setup.paperSize = work_sheet_style.PAPERSIZE_TABLOID
177+
work_sheet_style.page_setup.fitToWidth = 1
178+
work_sheet_style.page_setup.fitToHeight = 0
179+
180+
#
181+
# 与单元样式相反,命名样式是可变的
182+
#
183+
# 当您想一次将格式应用于许多不同的单元格时,它们很有意义。注意 将命名样式分配给单元后,对该样式的其他更改将 不会影响该单元。
184+
# 一旦将命名样式注册到工作簿中,就可以简单地通过名称来引用它。
185+
#
186+
187+
# 创建样式,注册与指定
188+
high_light_style = NamedStyle(name="highlight")
189+
high_light_style.font = Font(bold=True, size=20, color=colors.RED)
190+
boder_style = Side(style='thick', color="000000")
191+
high_light_style.border = Border(left=boder_style,
192+
top=boder_style,
193+
right=boder_style,
194+
bottom=boder_style)
195+
wb.add_named_style(high_light_style) # 工作簿注册样式,命名样式在首次分配给单元时也会自动注册
196+
ws_g8 = work_sheet_style['G8']
197+
ws_g8.style = high_light_style
198+
ws_g8.value = 'ws_g8'
199+
ws_h6 = work_sheet_style['H6']
200+
ws_h6.style = 'highlight' # 工作簿注册样式后,仅使用名称指定样式
201+
ws_h6.value = 'ws_h6'
202+
#
203+
# 使用内置样式
204+
# 请参考:
205+
# https://openpyxl.readthedocs.io/en/2.6/styles.html#using-builtin-styles
206+
#
207+
208+
wb.save(filename=file_name)
209+
210+
211+
if __name__ == "__main__":
212+
u'''读写Excel 文件'''
213+
file_name = generate_complex_or_fmt_file_name('excel', 'excel.xlsx')
214+
#
215+
# 写xlsx 文件
216+
#
217+
write_to_excel(file_name)
218+
#
219+
# 读xlsx 文件
220+
#
221+
# ready_to_excel(file_name)

case/excel/test_w_only_excel.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# import os
4+
from openpyxl import Workbook
5+
from openpyxl.cell import WriteOnlyCell
6+
from openpyxl.comments import Comment
7+
from openpyxl.styles import Font
8+
from openpyxl.utils import units
9+
from com.dvsnier.dir.common_dir import generate_complex_or_fmt_file_name
10+
from openpyxl.worksheet.properties import PageSetupProperties
11+
12+
13+
def write_to_excel(file_name):
14+
'the write data to excel file'
15+
if file_name is None or len(file_name.strip()) == 0:
16+
raise ValueError('the current file name is invaild.')
17+
18+
wb = Workbook(write_only=True) # 只写模式,工作簿
19+
work_sheet_of_default = wb.create_sheet()
20+
work_sheet_of_default.title = u"只写测试0" # 当前工作表title
21+
# 只写模式,Cell
22+
write_only_cell = WriteOnlyCell(work_sheet_of_default, value="hello world")
23+
write_only_cell.font = Font(name='Courier', size=20)
24+
# 注释具有text属性和author属性,必须同时设置
25+
write_only_cell.comment = Comment(text="cell comment",
26+
author="Author's Name")
27+
write_only_cell.comment.width = units.points_to_pixels(250)
28+
write_only_cell.comment.height = 250
29+
# 在只写工作簿中, 只能使用添加行 append(), 无法使用 cell()或在任意位置写入(或读取)单元 iter_rows()
30+
work_sheet_of_default.append([None, 3.14, write_only_cell])
31+
# 工作表注释
32+
work_sheet_of_comment = Comment(text="ws comment", author=u"资深好男人")
33+
work_sheet_of_default.comment = work_sheet_of_comment
34+
#
35+
# 工作表可用属性
36+
#
37+
# “enableFormatConditionsCalculation”
38+
# “filterMode”
39+
# “published”
40+
# “syncHorizontal”
41+
# “syncRef”
42+
# “syncVertical”
43+
# “transitionEvaluation”
44+
# “transitionEntry”
45+
# “tabColor”
46+
wsprops = work_sheet_of_default.sheet_properties # 工作表属性
47+
wsprops.tabColor = "1072BA"
48+
wsprops.filterMode = False
49+
wsprops.pageSetUpPr = PageSetupProperties(fitToPage=True,
50+
autoPageBreaks=False)
51+
wsprops.outlinePr.summaryBelow = False
52+
wsprops.outlinePr.applyStyles = True
53+
wsprops.pageSetUpPr.autoPageBreaks = True
54+
55+
wb.save(filename=file_name)
56+
57+
58+
if __name__ == "__main__":
59+
u'''只写Excel 文件'''
60+
file_name = generate_complex_or_fmt_file_name('excel', 'excel.xlsx')
61+
#
62+
# 写xlsx 文件
63+
#
64+
write_to_excel(file_name)

case/file/test_strip.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding:utf-8 -*-
2+
3+
import os
4+
5+
6+
def read_file(file_name):
7+
'the read file with reg'
8+
if file_name is not None:
9+
index = 0
10+
content = []
11+
for line in open(file_name, 'r'):
12+
if len(line.strip()) > 0:
13+
index += 1
14+
md_line_text = line.strip().split('[')
15+
line_text = str(
16+
str(index) + '. [' + md_line_text[1] +
17+
md_line_text[0]).encode('utf-8')
18+
content.append(line_text)
19+
# print(line_text)
20+
dir_name = os.path.dirname(file_name)
21+
dest_name = os.path.join(dir_name, 'out_asc.txt')
22+
if len(content) > 0:
23+
with open(dest_name, 'w') as file:
24+
for item in content:
25+
file.write(str(item).encode('utf-8') + '\n')
26+
27+
28+
if __name__ == "__main__":
29+
'''正则拆分按行读文件,写入MD 文件'''
30+
# 指定位置文件 ./out/asc.txt
31+
out_dir = os.path.join(os.getcwd(), 'out')
32+
file = os.path.join(out_dir, 'asc.txt')
33+
if os.path.isfile(file):
34+
read_file(file)

case/readExcel.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)