import win32com.client
import tempfile
import os
import shutil
def duplicate_and_delete_tasks():
try:
outlook = win32com.client.Dispatch(
"Outlook.Application"
)
namespace = outlook.GetNamespace(
"MAPI"
)
tasks_folder = namespace.GetDefaultFolder(13)
original_tasks = [task for task in tasks_folder.Items if task.
Class
== 48]
for task in original_tasks:
print(
"Task being processed: "
+ task.Subject)
new_task = tasks_folder.Items.Add(
"IPM.Task"
)
new_task.Subject = task.Subject
try:
new_task.RTFBody = task.RTFBody
except:
new_task.Body = task.Body
new_task.StartDate = task.StartDate
new_task.DueDate = task.DueDate
new_task.Categories = task.Categories
new_task.Importance = task.Importance
new_task.Status = task.Status
# if task.Attachments.Count > 0:
# temp_dir = tempfile.mkdtemp()
# for attachment in task.Attachments:
# try:
# attachment.SaveAsFile(os.path.join(temp_dir, attachment.FileName))
# new_task.Attachments.Add(os.path.join(temp_dir, attachment.FileName))
# except:
# pass
# shutil.rmtree(temp_dir)
new_task.Save()
print(
"New task created."
)
task.Delete()
print(
"Old task deleted."
)
print(
"All tasks have been successfully processed."
)
except Exception as e:
print(f
"An error occurred: {e}"
)
duplicate_and_delete_tasks()