-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapply_excel.py
168 lines (134 loc) · 4.22 KB
/
apply_excel.py
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
'''
作者: 张启卫
时间: 2018年12月5号
功能:Python 处理Excel 文件
参考文档: http://www.python-excel.org/
xlrd
pip install xlrd
wlwt
pip install xlwt
'''
import xlrd
import xlwt
import configparser
# 加载配置文件
config = configparser.ConfigParser()
# 写入配置文件
config.read('conf.ini', encoding='utf-8')
filename_prefix = config['suning_excel_filename']['filename_prefix']
filename_number = config['suning_excel_filename']['filename_number']
ret_file = 'suning-products.txt'
number = int(filename_number) + 1
print(filename_prefix)
print(filename_number)
filename_list = []
time_list = []
for i in range(1, number):
filename = filename_prefix + "-" + str(i) + ".xls"
filename_list.append(filename)
print(filename_list)
for filename in filename_list:
workbook = xlrd.open_workbook(filename)
sheet_names = workbook.sheet_names()
worksheet = workbook.sheet_by_name(sheet_names[0])
suningID_list = worksheet.col_values(2)
taobaoID_list = worksheet.col_values(7)
# 表示行数
i = 0
for suningID in suningID_list:
taobaoID = worksheet.cell(i, 7).value
status = worksheet.cell(i, 11).value
time = worksheet.cell(i, 12).value
if taobaoID != '' and status == '在售':
data = suningID.strip() + ";" + taobaoID.strip() + "\n"
with open(ret_file,'a') as file:
file.writelines(data)
file.close()
if time not in time_list:
time_filename = time + ".txt"
time_list.append(time_filename)
f = open(time_filename, 'a')
f.close()
if time_filename in time_list:
with open(time_filename,'a') as time_file:
time_file.writelines(data)
i += 1
'''
filename1 = '26120181204215349258-1.xls'
filename2 = '26120181204215349258-2.xls'
filename3 = '26120181204215349258-3.xls'
filename4 = '26120181204215349258-4.xls'
ret_file = 'suning-product.xls'
workbook1 = xlrd.open_workbook(filename1)
workbook2 = xlrd.open_workbook(filename2)
workbook3 = xlrd.open_workbook(filename3)
workbook4 = xlrd.open_workbook(filename4)
sheet_names1 = workbook1.sheet_names()
sheet_names2 = workbook2.sheet_names()
sheet_names3 = workbook3.sheet_names()
sheet_names4 = workbook4.sheet_names()
worksheet1 = workbook1.sheet_by_name(sheet_names1[0])
worksheet2 = workbook2.sheet_by_name(sheet_names2[0])
worksheet3 = workbook3.sheet_by_name(sheet_names3[0])
worksheet4 = workbook4.sheet_by_name(sheet_names4[0])
#苏宁商品编码
suningID_list1 = worksheet1.col_values(2)
suningID_list2 = worksheet2.col_values(2)
suningID_list3 = worksheet3.col_values(2)
suningID_list4 = worksheet4.col_values(2)
#淘宝商品编码
taobaoID_list1 = worksheet1.col_values(7)
taobaoID_list2 = worksheet2.col_values(7)
taobaoID_list3 = worksheet3.col_values(7)
taobaoID_list4 = worksheet4.col_values(7)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('product')
# 表示行数
i = 0
# 表示写入行数
j = 0
for suningID in suningID_list1:
taobaoID = worksheet1.cell(i, 7).value
status = worksheet1.cell(i, 11).value
time = worksheet1.cell(i, 12).value
if taobaoID != '' and status == '在售':
worksheet.write(j,0,suningID)
worksheet.write(j,1,taobaoID)
worksheet.write(j,2,time)
j += 1
i += 1
i = 0
for suningID in suningID_list2:
taobaoID = worksheet2.cell(i, 7).value
status = worksheet2.cell(i, 11).value
time = worksheet2.cell(i, 12).value
if taobaoID != '' and status == '在售':
worksheet.write(j,0,suningID)
worksheet.write(j,1,taobaoID)
worksheet.write(j,2,time)
j += 1
i += 1
i = 0
for suningID in suningID_list3:
taobaoID = worksheet3.cell(i, 7).value
status = worksheet3.cell(i, 11).value
time = worksheet3.cell(i, 12).value
if taobaoID != '' and status == '在售':
worksheet.write(j,0,suningID)
worksheet.write(j,1,taobaoID)
worksheet.write(j,2,time)
j += 1
i += 1
i = 0
for suningID in suningID_list4:
taobaoID = worksheet4.cell(i, 7).value
status = worksheet4.cell(i, 11).value
time = worksheet4.cell(i, 12).value
if taobaoID != '' and status == '在售':
worksheet.write(j,0,suningID)
worksheet.write(j,1,taobaoID)
worksheet.write(j,2,time)
j += 1
i += 1
workbook.save(ret_file)
'''