forked from gxcuizy/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenpyxl.py
48 lines (46 loc) · 1.14 KB
/
openpyxl.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import openpyxl
from openpyxl import load_workbook
# 程序主入口
if __name__ == "__main__":
# 读取excel数据
wb = load_workbook('test.xlsx', data_only=True)
sheet = wb.active
# 写表excel数据
write_wb = openpyxl.Workbook()
write_sheet = write_wb.active
print('开始写入excel')
# 表头信息
cxcel_title = [
'title_1',
'title_2',
'title_3',
'title_4',
'title_5',
'title_6'
]
# 表头写入Excel中
write_sheet.append(cxcel_title)
# 循环写入旧Excel数据
for row in sheet.rows:
# 行信息
title_1 = row[0].value
title_2 = row[1].value
title_3 = row[2].value
title_4 = row[3].value
title_5 = row[4].value
title_6 = row[5].value
excel_row = [
title_1,
title_2,
title_3,
title_4,
title_5,
title_6
]
# 写入Excel中
write_sheet.append(excel_row)
# 生成excel文件
write_wb.save('new_test.xlsx')
print('写入excel完成!')