-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_test3.py
66 lines (54 loc) · 2.04 KB
/
loop_test3.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
"""use win32com to calculate NPV"""
import win32com
from win32com.client import Dispatch, constants
import os
import pandas as pd
# 获取当前脚本路径
def getScriptPath():
nowpath = os.path.split(os.path.realpath(__file__))[0]
print(nowpath)
return nowpath
def main(excel_name='敏感性分析1.1(1).xlsx'):
circular_table = []
app = win32com.client.Dispatch('Excel.Application')
# 后台运行,不显示,不警告
app.Visible = 0
app.DisplayAlerts = 0
WorkBook = app.Workbooks.Open(os.path.join(getScriptPath(), excel_name))
WorkSheet = WorkBook.Worksheets('基本信息')
# process in batch
age_list = range(20, 66) # age range from 20 to 65
gen_list = [0, 1] # gender list
time_flags = [0, 1]
sa_levels = [1, 2, 3]
for age in age_list:
print('age: {}'.format(age))
circular_table_row = []
for gender in gen_list:
for flag in time_flags:
for level in sa_levels:
# set age
WorkSheet.Range('B2').Value = age
# set gender
WorkSheet.Range('B3').Value = gender
# set time flag
WorkSheet.Range('B4').Value = flag
# set sa level
WorkSheet.Range('B5').Value = level
# read NPV
NPV = WorkSheet.Range('E3').Value
# print(WorkSheet.Range('B2').Value,
# WorkSheet.Range('B3').Value,
# WorkSheet.Range('B4').Value,
# WorkSheet.Range('B5').Value,
# WorkSheet.Range('B6').Value,
# NPV)
circular_table_row.append(NPV)
circular_table.append(circular_table_row)
# convert to DataFrame
circular_table = pd.DataFrame(circular_table)
circular_table.to_excel('result.xlsx', index=False, header=False)
WorkBook.Close()
app.Quit()
if __name__ == '__main__':
main()