[英]Repeatibility issue with win32com.client
我編寫了一些代碼來自動化一些工作,其中包括從特定的Outlook文件夾中提取Excel附件,將其存儲在位置(文件名中帶有日期時間),然后從中提取一些數據。
我有使用Jupyter筆記本的完整功能代碼。 但是,當我創建一個.exe(使用auto-py-to-exe)並運行時,我在特定方法SentOn上收到錯誤消息。 關於什么原因的任何指示?
我嘗試使用限制,但如果未明確輸入日期和時間,則無法使用。 我想使用自動定義的參數。
不起作用:
lastWk_dt = dt.timedelta(days=-7) + currentWk
lastWk = lastWk_dt.strftime("%m/%d/%Y %I:%M %p") ## convert to str and format
messages = calloutFolder.Items.restrict(f"[SentOn] > {lastWk}")
可以,但是沒有用:
messages = calloutFolder.Items.restrict("[SentOn] > '08/13/2019 06:00 AM'")
import os
import shutil
import win32com.client
import datetime as dt
## defining timeline of data extraction
currentWk = dt.datetime.now().date()
lastWk = dt.timedelta(days=-7) + currentWk
## creating folders for storing attachments
cwd = os.getcwd()
savePath = cwd +"/"+str(currentWk)
try:
shutil.rmtree(savePath)
os.mkdir(savePath)
except OSError as e:
os.mkdir(savePath)
## reading MS Outlook subfolder called "SOME FOLDER" in inbox
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
calloutFolder = outlook.GetDefaultFolder(6).Folders["SOME FOLDER"]
messages = calloutFolder.Items
for message in messages:
if (message.Senton.date() <= currentWk) and (message.Senton.date() >= lastWk):
# source of bug, wherever I place SentOn it bugs out
attachment = message.Attachments.Item(1)
# changes message to read
message.Unread = False
## saving attachments
for attachment in message.Attachments:
attachment.SaveAsFile(os.path.join(savePath, str(attachment)))
break
預期結果是將各種Excel文件重命名並保存在位置“ savePath”中
lastWk
是datetime.datetime對象。 嘗試
messages = calloutFolder.Items.restrict(f"[SentOn] > '{lastWk}'")
使用f字符串(在3.6及更高版本中可用)。 否則使用str.format()
方法。
您可能需要將lastWk
明確轉換為特定格式。 在這種情況下,請查看datetime.datetime.strftime()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.