【问题标题】:Sharepoint excel data take into Pandas data frame without downloadingSharepoint excel 数据无需下载即可进入 Pandas 数据框
【发布时间】:2021-06-01 21:54:01
【问题描述】:

我需要将我的 SharePoint excel 文件放入 pandas 数据框中,因为我需要使用 python 对该 excel 文件进行分析。访问 SharePoint 我使用下面的代码,它可以工作。从下面的代码中,我可以访问位于 SharePoint 中的我的 excel 文件。现在我想将我的 excel 文件放入 pandas 数据框中。那么我该如何修改下面的代码?

from office365.sharepoint.client_context import ClientContext

SP_SITE_URL ='https://asdfgh.sharepoint.com/sites/ABC/'
SP_DOC_LIBRARY ='Publications'
USERNAME ='asd@fgh.onmicrosoft.com'
PASSWORD ='******' 

# 1. Create a ClientContext object and use the user’s credentials for authentication 
ctx =ClientContext(SP_SITE_URL).with_user_credentials(USERNAME, PASSWORD) 

# 2. Read file entities from the SharePoint document library 
files = ctx.web.lists.get_by_title(SP_DOC_LIBRARY).root_folder.files 
ctx.load(files)
ctx.execute_query()

# 3. loop through file entities
for filein files: 
  # 4. Access the file object properties 
  print(file.properties['Name'], file.properties['UniqueId'])
  # 5. Access list item object through the file object 
  item = file.listItemAllFields
  ctx.load(item) 
  ctx.execute_query() 
  print('Access metadata - Category: {0}, Status: {1}'.format(item.properties['Category'], item.properties['Status']))

# 4. The Output: 
# File Handling in SharePoint Document Library Using Python.docx 77819f08-5fbe-450f-9f9b-d3ae2862cbb5
# Access metadata - Category: Python, Status: Submitted 

【问题讨论】:

    标签: python excel pandas azure sharepoint


    【解决方案1】:

    为了通过它进行操作,文件将需要存在于系统的内存中。

    找到文件的路径 - 它应该在你已经存在的文件的元数据中。

    使用以下库:

    from office365.sharepoint.files.file import File 
    

    您可以使用以下代码继续并将其存储在内存中并从 Panda 数据帧中读取。

    response = File.open_binary(ctx, url)
    
    bytes_file_obj = io.BytesIO()
    bytes_file_obj.write(response.content)
    bytes_file_obj.seek(0) #set file object to start
    
    df = pd.read_excel(bytes_file_obj, sheetname = <Sheetname>)
    

    【讨论】:

    • 跟进看看上面的解决方案有没有帮助?
    猜你喜欢
    • 2021-11-21
    • 2022-12-11
    • 2023-01-18
    • 2019-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多