Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当xlsm文件关闭后再次打开时,使用Excel VBA发送yagmail不起作用_Python_Excel_Vba_Yagmail - Fatal编程技术网

Python 当xlsm文件关闭后再次打开时,使用Excel VBA发送yagmail不起作用

Python 当xlsm文件关闭后再次打开时,使用Excel VBA发送yagmail不起作用,python,excel,vba,yagmail,Python,Excel,Vba,Yagmail,这个问题可能与Python、Excel和VBA有同等的关系。我有可以使用Excel和VBA开始工作的Python代码,但是,一旦关闭并再次打开启用宏的工作簿(xlsm),Python代码就无法再成功运行 下面是我的Python代码,当提供有效的用户名gmail地址、密码和收件人地址时,将其保存为“.py”文件并手动启动时,该代码对我有效: #Based on: https://realpython.com/python-send-email/#option-1-setting-up-a-gmai

这个问题可能与Python、Excel和VBA有同等的关系。我有可以使用Excel和VBA开始工作的Python代码,但是,一旦关闭并再次打开启用宏的工作簿(xlsm),Python代码就无法再成功运行

下面是我的Python代码,当提供有效的用户名gmail地址、密码和收件人地址时,将其保存为“.py”文件并手动启动时,该代码对我有效:

#Based on: https://realpython.com/python-send-email/#option-1-setting-up-a-gmail-account-for-development

import yagmail

username = "x@gmail.com"
password = 'xxx'
receiver = ["y@gmail.com", "z.com"]

Subj = "Test - Daily update log tool")
body = "xxx Update xxx."
filename = ['Upload/Stats.xlsx', 'Upload/4 Diagramme.jpg']

yagmail.register(username, password)

yag = yagmail.SMTP(username)
yag.send(
    to=receiver,
    subject= Subj,
    contents=body, 
    attachments=filename,
)
当我打开一个新的Excel工作簿并将以下VBA代码粘贴到一个新模块中时,它也可以工作,将工作簿保存在一个包含python文件的文件夹中(以及一个名为“Upload”的文件夹,其中包含两个附件),然后在Excel Visual Basic编辑器中按F5运行它:

Sub YagmailStatsDiagramme()

Dim objShell As Object
Dim PythonExe, PythonScript As String

'Create a new Object shell.
Set objShell = VBA.CreateObject("Wscript.Shell")

PythonScript = Chr(34) & ThisWorkbook.path & "\Yagmail Stats Diagramme.py" & Chr(34)

'Run the Python Script
objShell.Run PythonScript

End Sub
电子邮件发送正确。但是,当关闭并再次打开此工作簿时,在VBA窗口中按F5会突然出现Python类型错误,表示我没有使用有效的文件路径。我在装有Windows 10、office 2016和Python 3.8的机器上进行了测试,结果显示以下错误消息:

我在另一台装有Windows 10、Office 2019和Anaconda Python 3.7的机器上得到了同样的结果:

第一台机器上也安装了Anaconda,但默认为单独安装的Python 3.8。在第二台机器上也安装了Python 3.9,但它默认为Anaconda Python 3.7,尽管Anaconda目录不在系统变量中,尽管Python 3.9目录是系统变量的一部分。正在添加“”。#!代码开头的Python3.9“”不会改变这一点。手动运行.py文件并从python 3.9中选择python.exe打开它时,不会发送电子邮件

帮助解开这个谜团真的很受欢迎。多谢各位