簡體   English   中英

ImportError:DLL加載失敗:導入win32com時%1不是有效的Win32應用程序

[英]ImportError: DLL load failed: %1 is not a valid Win32 application while importing win32com

我嘗試了這封電子郵件發送示例代碼

# -*- 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 vs x64問題。 如果使用64位python,請導入64位dll,如果使用32位python,請導入32位dll。

Edit2:我認為就是您想要的。 請參閱了解詳情。

您是否安裝了適用於Python的Win32擴展? 確保為您的系統和Python版本選擇正確的安裝程序,否則可能無法正常工作。

我按照Trapli的指示重新安裝了pywin32(32位版本),還安裝了psutil模塊並重新啟動了系統。 問題已解決,現在可以發送電子郵件了。 謝謝大家的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM