Uncategorized

SharePoint list and Excel file interaction using Python


I would like to automatically update a SharePoint list when data is updated in an Excel file.
I am providing the code snippet. When I run the code there is no noticeable error but the SharePoint list is not getting updated. Why is it happening? Any help would be appreciated.

# Specify the Excel file path
excel_file_path = "C:\\Users\\VISSAJEE\\Desktop\\exel_shp.xlsx"

# Read the Excel file into a pandas dataframe
df = pd.read_excel(excel_file_path) 

# Retrieve the SharePoint list items 
list_name = config.get('SharePoint', 'list_name')
list_items = ctx.web.lists.get_by_title(list_name).items.get()
ctx.load(list_items)
ctx.execute_query()

# Compare Data and Update SharePoint List
for index, row in df.iterrows():
    title_to_find = row["Title"]  # Assuming "Title" is a column in both Excel and SharePoint
    matching_item = next((item for item in list_items if item.properties["Title"] == title_to_find), None)

    if matching_item:
        # Compare and update other columns if necessary
        if matching_item.properties["Athena_x0020_Request"] != row["Athena Request"]:
            matching_item.set_property("Athena_x0020_Request", row["Athena Request"])
        if matching_item.properties["Affected_x0020_documents"] != row["Affected documents"]:
            matching_item.set_property("Affected_x0020_documents", row["Affected documents"])
        if matching_item.properties["Modified_x0020_By"] != row["Modified By"]:
            matching_item.set_property("Modified_x0020_By", row["Modified By"])
        if matching_item.properties["Item_x0020_Type"] != row["Item Type"]:
            matching_item.set_property("Item_x0020_Type", row["Item Type"])
        if matching_item.properties["Path"] != row["Path"]:
            matching_item.set_property("Path", row["Path"])

        matching_item.update()

        # Print a message indicating that SharePoint is being updated
        print(f"SharePoint item updated for Title: {title_to_find}")

# Execute Queries to Update SharePoint List
ctx.execute_query()

I tried running the code but the SharePoint list is not being updated when the datasin corresponding columns of Excel file were being added.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *