python - 导入错误: DLL load failed: %1 is not a valid Win32 application while importing win32com

标签 python win32com

我尝试发送此电子邮件 sample code :

# -*- coding: utf-8 -*-
"""
Created on Wed Sep 21 15:36:00 2016

@author: Deepesh.Singh
"""

import win32com.client as win32
import psutil
import os
import subprocess

# Drafting and sending email notification to senders. You can add other senders' email in the list
def send_notification():
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = 'abc@xzs.com; bhm@ert.com', 
    mail.Subject = 'Sent through Python'
    mail.body = 'This email alert is auto generated. Please do not respond.'
    mail.send

# Open Outlook.exe. Path may vary according to system config
# Please check the path to .exe file and update below

def open_outlook():
    try:
        subprocess.call(['C:\Program Files\Microsoft Office\Office15\Outlook.exe'])
        os.system("C:\Program Files\Microsoft Office\Office15\Outlook.exe");
    except:
        print("Outlook didn't open successfully")

# Checking if outlook is already opened. If not, open Outlook.exe and send email
for item in psutil.pids():
    p = psutil.Process(item)
    if p.name() == "OUTLOOK.EXE":
        flag = 1
        break
    else:
        flag = 0

if (flag == 1):
    send_notification()
else:
    open_outlook()
    send_notification()

但是当我在命令提示符下运行代码时不断遇到以下错误:

C:\<>\Desktop\Exp>python sendemail.py
Traceback (most recent call last):
  File "sendemail.py", line 40, in <module>
    import win32com.client
  File "C:\Python27\lib\site-packages\win32com\__init__.py", line 5, in <module>
    import win32api, sys, os
ImportError: DLL load failed: %1 is not a valid Win32 application.

有人可以指导我如何修复此错误吗?还是他们有更好的方法来做到这一点?

谢谢。

最佳答案

这可能是 x86 与 x64 的问题。如果您使用64位python,则导入64位dll,如果您使用32位python,则导入32位dll。

编辑2: 我认为this就是您正在寻找的。请参阅this了解详情。

关于python - 导入错误: DLL load failed: %1 is not a valid Win32 application while importing win32com,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640587/

相关文章:

python - Google Colab-ValueError : Mountpoint must be in a directory that exists

python - 为什么我的 pygame 在 Mac 上加载到空白屏幕?

c# - C# 和 Python 之间的命名管道

python3和pywin32关闭excel

python - .doc 到 pdf 使用 python

python - 使用 Python 将自动筛选应用到工作表

python - 没有名为 'win32api' 的模块

python - 减少 x 轴上标签为日期的刻度数

python - 如何加速 numpy 数组上的函数

c++ - 在列表或菜单中列出捕获设备