Using O365 Python API to download and update Sharepoint Site List items

240 Views Asked by At

please does anyone knows how to download and update SharePoint Site List items with corresponding columns name and fields/values using O365 Python API ?

I am using the Sharepoint python-o365 package https://github.com/O365/python-o365/blob/ffcea65c82c2d03dac8fd756ebe050591b371305/O365/sharepoint.py

There is get_items() method but it only returns the list object and the column_names and corresponding fields or values. I tried below code but it doesn't work expected

from O365.sharepoint import Sharepoint, Site

def get_list_items(self, list_name):
        target_site = self.site
        target_list = target_site.get_list_by_name(display_name=list_name)
        list_items = target_list.get_items()

        data = []
        columns = []  # Collect column names
        
        for item in list_items:
            item_data = {}  # Collect item data for each row
            
            for column_name, column_value in item.properties.items():
                if column_name not in columns:
                    columns.append(column_name)
                
                # Store column values in the item_data dictionary
                item_data[column_name] = column_value
            
            data.append(item_data)  # Append the item_data to the data list

        # Create a DataFrame using the collected data and columns
        df = pd.DataFrame(data, columns=columns)
        
        return df

I want to download SharePoint Site List into pandas dataframe?

1

There are 1 best solutions below

0
On
spo_data = [item.properties for item in self.target_list.items.paged(500).get().execute_query()]
self.df = pd.DataFrame(spo_data)

This is what I am using. List comprehension in order to get all the items in the list. I am using paged as SPO Lists are paginated and only pull the first 500 items. Once that is complete, I create the DataFrame from the list.