我能够从 Excel VBA 运行 Python 程序。 由于这个 Pyhton 程序可以从 Excel VBA 运行,也可以在独立模式下运行,所以我需要 Pyhton 程序中的某些内容来找出它的运行方式(因为我必须在执行中应用一些小的更改)。
我可以在Python程序中查询/识别什么样的参数(或类似的东西)来确定它是否被Excel VBA调用?
我宁愿不将参数从 Excel VBA 传递到 Python(这看起来并不明显)。
经过一番研究,我写了一个符合我期望的解决方案。 但是,如果有人知道如何区分“关闭”和“锁定在只读模式”状态,则可以改进。
同时,这是我的输出:
import os
def file_test(f):
if os.path.exists(os.path.split(f)[0]):
# folder exists, useless to tell the user
if os.access(f, os.F_OK):
# file exists, useless to tell the user
try:
os.rename(f, f)
return "file is closed or locked" # when file is either closed or opened in read-only mode
except:
return "file is opened" # file is opened in write mode
else:
return "file is unknown" # file does not exist
else:
return "folder is unknown" # folder dos not exist
f = 'C:/Users/didie/OneDrive/Bureau/file_test.xlsb'
print(file_test(f))
希望有帮助...