运行可执行文件以获取Outlook附件

发布于 2025-01-17 18:54:27 字数 3423 浏览 1 评论 0原文

我编写了从 [email protected] 下载 Outlook 附件的 Python 代码将它们保存在special_folder中。当我在 Spyder 中运行它时效果很好,但是当我将其转换为可执行文件并运行它时,附件不会下载,但不会引发错误。我很困惑可能是什么问题。

import win32com.client as win
import datetime as dt


# Set up connection to outlook
outlook = win.Dispatch("Outlook.Application").GetNamespace("MAPI")

# Go to the inbox folder
inbox = outlook.GetDefaultFolder(6)

# Record the messages
messages = inbox.Items

# Get the first message
message = messages.GetLast()

# Record today's date
today = dt.date.today()

# Initialize email date to today
email_date = today

# Get yesterday's date
yesterday = today - dt.timedelta(days = 1)

# Initialize count at 0
count = 0

# Record the number of emails read
emails_read = 0

# Record the number of tries
tries = 0

# Record the number of excepts
excepts = 0

# Was the break reached?
break_reached = 0

# While the count is less than three
while count < 4:

  emails_read += 1
  
  try:

      # Record the email address
      email_address = message.SenderEmailAddress
      
      # Record the date when the message was sent
      email_date = message.senton
        
      # Convert the date to a dt.date object
      email_date = dt.date(year = email_date.year, month = email_date.month, day = email_date.day)
        
      # If the email is from [email protected] and the email was send yesterday... 
      if (email_address == '[email protected]') & ((email_date == yesterday)|(email_date == today)):
            
          # Record the attachment  
          attachments = message.Attachments
              
          # Get the first attachment
          attachment = attachments.Item(1)
              
          # Get the name of the attachment
          attachment_name = str(attachment)
              
          # Save the attachment in my ICE folder
          attachment.SaveASFile(particular_location + attachment_name)
              
          # Add 1 to the count
          count += 1
              
      # Move on to the next message
      message = messages.GetPrevious()
      
      tries += 1
       
  # ... when not possible...
  except:
        
      # ... move on to the next message            
      message = messages.GetPrevious()
      
      excepts += 1
          
    
  if (emails_read > 1e4)|(email_date == dt.date.today() - dt.timedelta(days = 2)):
      
      break_reached += 1
      
      break
  
# Create a log dictionary
log_dict = {'count':count, 'emails_read':emails_read, 'tries':tries, 'excepts':excepts, 
            'break_reached':break_reached, 'email_date':email_date.strftime("%m-%d-%y")}

   
# Record the hyperparameters from the cross-validation
with open(other_location + 'log.txt', 'w') as f: 
    
    # Look over the dictionary of best hyperparameters
    for key, value in log_dict.items(): 
        
        # Print key: value (space)
        f.write('%s:%s\n' % (key, value))

我已将日志添加到上面显示的代码中。日志中写道:

计数:0 emails_read:10001 尝试:0 例外:10001 Break_reached:1 email_date:03-30-22

所以,事实证明我总是在 try 情况下收到错误,并且它转到 except 。

I've written Python code that downloads Outlook attachments from [email protected] and saves them in particular_folder. It works great when I run it in Spyder, but when I convert it to an executable and run it, the attachments aren't downloaded, though no errors are thrown. I'm very confused about what the problem could be.

import win32com.client as win
import datetime as dt


# Set up connection to outlook
outlook = win.Dispatch("Outlook.Application").GetNamespace("MAPI")

# Go to the inbox folder
inbox = outlook.GetDefaultFolder(6)

# Record the messages
messages = inbox.Items

# Get the first message
message = messages.GetLast()

# Record today's date
today = dt.date.today()

# Initialize email date to today
email_date = today

# Get yesterday's date
yesterday = today - dt.timedelta(days = 1)

# Initialize count at 0
count = 0

# Record the number of emails read
emails_read = 0

# Record the number of tries
tries = 0

# Record the number of excepts
excepts = 0

# Was the break reached?
break_reached = 0

# While the count is less than three
while count < 4:

  emails_read += 1
  
  try:

      # Record the email address
      email_address = message.SenderEmailAddress
      
      # Record the date when the message was sent
      email_date = message.senton
        
      # Convert the date to a dt.date object
      email_date = dt.date(year = email_date.year, month = email_date.month, day = email_date.day)
        
      # If the email is from [email protected] and the email was send yesterday... 
      if (email_address == '[email protected]') & ((email_date == yesterday)|(email_date == today)):
            
          # Record the attachment  
          attachments = message.Attachments
              
          # Get the first attachment
          attachment = attachments.Item(1)
              
          # Get the name of the attachment
          attachment_name = str(attachment)
              
          # Save the attachment in my ICE folder
          attachment.SaveASFile(particular_location + attachment_name)
              
          # Add 1 to the count
          count += 1
              
      # Move on to the next message
      message = messages.GetPrevious()
      
      tries += 1
       
  # ... when not possible...
  except:
        
      # ... move on to the next message            
      message = messages.GetPrevious()
      
      excepts += 1
          
    
  if (emails_read > 1e4)|(email_date == dt.date.today() - dt.timedelta(days = 2)):
      
      break_reached += 1
      
      break
  
# Create a log dictionary
log_dict = {'count':count, 'emails_read':emails_read, 'tries':tries, 'excepts':excepts, 
            'break_reached':break_reached, 'email_date':email_date.strftime("%m-%d-%y")}

   
# Record the hyperparameters from the cross-validation
with open(other_location + 'log.txt', 'w') as f: 
    
    # Look over the dictionary of best hyperparameters
    for key, value in log_dict.items(): 
        
        # Print key: value (space)
        f.write('%s:%s\n' % (key, value))

I've added a log to the code which is shown above. The log reads:

count:0 emails_read:10001 tries:0 excepts:10001 break_reached:1
email_date:03-30-22

So, it turns out I'm always getting an error in the try case and it goes to the except.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無心 2025-01-24 18:54:27

有很多原因会导致您的代码无法将附加文件保存到磁盘上。我举几个例子:

  1. Outlook 是一个单例。您不能同时运行 Outlook 应用程序的两个实例。因此,如果加载的实例在不同的安全内容下运行,您的代码可能会失败。

  2. 无法访问目标文件夹。可能是写作权限不足、缺席等。

  3. 未找到与代码中定义的条件相对应的 Outlook 项目。

我们可以继续在那里列出假设。但只有日志文件才能揭示您所面临的问题。

There are plenty reasons why your code could fail to save attached files on the disk. I'll name a few:

  1. Outlook is a singleton. You can't run two instances of the Outlook application at the same time. So, if the loaded instance is run under a different security content your code may fail.

  2. The target folder is not reachable. It can be insufficient privileges for writing, absence and etc.

  3. Outlook items that correspond to the defined conditions in the code are not found.

We can continue listing assumptions there. But only a log file can shed some light on the problem you faced with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文