-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyexcel.py
More file actions
71 lines (55 loc) · 2.03 KB
/
pyexcel.py
File metadata and controls
71 lines (55 loc) · 2.03 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# read and write 2003 excel --> xls
import xlrd
import xlwt
# read and write 2007 excel --> xlsx
import openpyxl
def write03Excel(path):
wb = xlwt.Workbook()
sheet = wb.add_sheet("2003test")
value = [["名称", "价格", "出版社", "语言"],
["如何高效读懂一本书", "22.3", "机械工业出版社", "中文"],
["暗时间", "32.4", "人民邮电出版社", "中文"],
["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]
for i in range(0, 4):
for j in range(0, len(value[i])):
sheet.write(i, j, value[i][j])
print(i, j)
wb.save(path)
print("写入数据成功!")
def read03Excel(path):
workbook = xlrd.open_workbook(path)
sheets = workbook.sheet_names()
worksheet = workbook.sheet_by_name(sheets[0])
for i in range(0, worksheet.nrows):
# row = worksheet.row(i)
for j in range(0, worksheet.ncols):
print(worksheet.cell_value(i, j), "\t", end="")
def write07Excel(path):
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = '2007test'
value = [["名称", "价格", "出版社", "语言"],
["如何高效读懂一本书", "22.3", "机械工业出版社", "中文"],
["暗时间", "32.4", "人民邮电出版社", "中文"],
["拆掉思维里的墙", "26.7", "机械工业出版社", "中文"]]
for i in range(0, 4):
for j in range(0, len(value[i])):
sheet.cell(row=i + 1, column=j + 1, value=str(value[i][j]))
wb.save(path)
print("写入数据成功!")
def read07Excel(path):
wb = openpyxl.load_workbook(path)
# sheet = wb.get_sheet_by_name('2007test')# old edition
sheet = wb['2007test']
for row in sheet.rows:
for cell in row:
print(cell.value, "\t", end="")
print()
file_2003 = './2003.xls'
file_2007 = './2007.xlsx'
write03Excel(file_2003)
read03Excel(file_2003)
write07Excel(file_2007)
read07Excel(file_2007)