编程生成一个Excel文件,在代码中向Excel找那个填入课程名,学分及分数,计算GPA之后填入Excel

Posted by 石坤 on 2019-02-04

编程生成一个Excel文件,在代码中向Excel找那个填入课程名,学分及分数,计算GPA之后填入Excel

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
from openpyxl import *

book = Workbook()
sheet = book.active

rows = (
('课程名', '数学', 'Python'),
('分数', 99, 90),
('学分', 4, 2),
('GPA', 0.0)
)
for row in rows:
sheet.append(row)

book.save('grade.xlsx')

book = load_workbook('grade.xlsx')
sheet = book.active

a1 = sheet['B2'].value
a2 = sheet['B3'].value
w1 = sheet['B2'].value
w2 = sheet['B3'].value
gpa = (a1*w1+a2*w2)/(w1+w2)

book = Workbook()
sheet = book.active
for row in rows:
sheet.append(row)

sheet['B4'] = gpa

book.save('grade.xlsx')

参考资料:http://zetcode.com/articles/openpyxl/