Iterating through each line from the excel file I created, I constantly get the same error at line 2555 of the excel spreadsheet. Even added text formatting. Was able to copy and paste the data straight into Microsoft Project.
An error occurred during adding tasks to MS Project: (-2147352567, 'Exception occurred.', (0, None, 'The argument value is not valid.', 'C:\Program Files\Microsoft Office\root\Office16\VBAPJ.CHM', 131074, -2146827187), None)
import pandas as pdfrom openpyxl import load_workbookfrom openpyxl.styles import Fontfrom datetime import datetimeimport win32com.clientdef add_tasks_to_project(excel_file_path, project_file_path): try: # Connect to MS Project app_project = win32com.client.Dispatch("MSProject.Application") app_project.Visible = True project = app_project.FileOpen(project_file_path) # Get the active project active_project = app_project.ActiveProject # Prompt user before adding tasks`your text` to MS Project user_input = input("Do you want to add tasks to MS Project? (yes/no): ").lower() if user_input != 'yes': print("Data will not be added to MS Project.") return # Load Excel workbook workbook = load_workbook(excel_file_path) sheet = workbook.active # Format column A as text for row in sheet.iter_rows(min_row=2, min_col=1, max_col=1): for cell in row: cell.number_format = '@' # '@' is the code for text format # Iterate through column A starting from the second row (excluding A1) for i, cell in enumerate(sheet['A'], start=2): task_name = cell.value print(f"Processing row {i}, task name: {task_name}") if task_name: print(f"Adding task: {task_name}") # Add task to the project task = active_project.Tasks.Add(Name=task_name) else: print(f"Empty cell found at row {i}, loop terminated.") # Save and close the project active_project.Save() active_project.Close() print("Tasks added successfully to MS Project.") except Exception as e: print("An error occurred while adding tasks to MS Project:", str(e)) if 'app_project' in locals(): app_project.Quit()if __name__ == "__main__": # Your code to call the function goes here````I tried to format the data as text, still ran into the same error, tried to add some print functions to find out what might cause the error but didn't help. Still learning Python, so still don't know what's going wrong.