Skip to content

Commit

Permalink
'添加Excel处理类'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuizhongyi committed Aug 14, 2018
1 parent 60d8658 commit 1750eea
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions excel/openpyxl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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完成!')

0 comments on commit 1750eea

Please sign in to comment.