从excel vba运行python脚本时出错

2024-06-10 11:30:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python脚本读取excel工作表中的单元格值,我的run按钮位于该工作表中。当我从IDE运行脚本时,它工作正常,当我尝试用我的按钮运行它时,它无法从单元格中获取值(脚本停止运行)。 下面是我的代码示例:

VBA示例:

Sub RunPython()
    Ret_Val = shell("C:\Users\s1618\AppData\Local\Programs\Python\Python37\python.exe" & " C:\Users\s1618\Desktop\untitled\test.py")
End Sub

PYTHON脚本示例:

import xlrd

book = xlrd.open_workbook("data.xlsm")
sheet = book.sheet_by_name("data")
cell_value = sheet.cell_value(1, 3)
return cell_value

有什么建议吗


Tags: run脚本示例datavaluecellideexcel