我有一个python脚本,该脚本使用 Configparser 模块获取连接参数。
当我通过cmd提示符调用此脚本时,它可以按预期工作:
C:\Users\vino>C:\Python37-32\python.exe C:\Python37-32\dim.py
我计划通过VBA做到这一点:
因此,我创建了以下Excel宏(2010 Excel):
Public Sub StartExeWithArgument()
Dim strProgramName As String
Dim strArgument As String
strpythonpath = "C:\Python37-32\python.exe"
strProgramName = "C:\Python37-32\dim.py"
Call Shell("""" & strpythonpath & """ """ & strProgramName & """", vbNormalFocus)
End Sub
但是上面的代码抛出错误:
Traceback (most recent call last): File "dim.py", line 16 in <module> File "configparser.py", line 958, in __getitem__ KeyError: 'SIT' [8700] Failed to execute script dim
configparser的输入文件:
[SIT]
address=localhost
port=9049
user=vino
password=apple
ssl=False
Python代码:
import configparser
import os
import sys
cube_name = 'Sales'
config = configparser.ConfigParser()
from pathlib import Path
cwd = Path().absolute()
config.read(os.path.join(cwd, 'config.ini'))
metadata_file = os.path.join(cwd, cube_name+'.xlsx')
import pandas as pd
from TM1py.Services import TM1Service
from TM1py.Utils import Utils
# Connection to TM1. Needs Address, Port, Credentials, and SSL
with TM1Service(**config['SIT']) as tm1:
all_cubes = tm1.cubes.get(cube_name)
dims = all_cubes.dimensions
with pd.ExcelWriter(metadata_file) as writer:
for dim in dims:
df = []
dimension = tm1.dimensions.get(dim)
for hierarchy in dimension:
for element in hierarchy:
df.append(element.name)
pd.DataFrame(df).to_excel(writer, sheet_name=dim, header=False, index=False)