-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtomerge.py
More file actions
44 lines (36 loc) · 1.29 KB
/
Copy pathtomerge.py
File metadata and controls
44 lines (36 loc) · 1.29 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
# -*- coding: UTF-8 -*-
import os
import openpyxl
def savexls(src, dst):
srcwb = openpyxl.load_workbook(src)
srcws = srcwb.get_sheet_by_name(srcwb.get_sheet_names()[0])
if os.path.exists(dst):
dstwb = openpyxl.load_workbook(dst)
dstws = dstwb.get_sheet_by_name(dstwb.get_sheet_names()[0])
dst_max_row = dstws.max_row # 最大行数
else:
dst_max_row = 0
openpyxl.Workbook().save(dst)
dstwb = openpyxl.load_workbook(dst)
dstws = dstwb.get_sheet_by_name(dstwb.get_sheet_names()[0])
for i, row in enumerate(srcws.iter_rows()):
for j, col in enumerate(row):
dstws.cell(row=i + 1 + dst_max_row, column=j + 1, value=col.value)
dstwb.save(dst)
def file_name(file_dir):
L = []
for root, _, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.xlsx':
L.append(os.path.join(root, file))
return L
def main():
srcfile = file_name(os.getcwd())
print(u'merge')
dstfile = os.path.dirname(os.path.abspath(__file__)).split('\\')[-1] + u'.xlsx'
assert not os.path.exists(dstfile), "file already existed"
for _, x in enumerate(srcfile):
savexls(x, dstfile) # 尾插
print(u'all done')
if __name__ == '__main__':
main()