繁体   English   中英

尝试将 pandas dataframe 保存为 excel 文件但出现导入错误

[英]Trying to save pandas dataframe as an excel file but getting import error

Im trying to save my pandas dataframe as an excel sheet in my sharepoint site using Office365-REST-Python-Client API.

我参考了以下链接将Pandas dataframe 保存到 SharePoint 位置为 Z628CB5675FF524F3E719B73

用户(感谢紫罗兰)说在 memory 中创建一个 BytesIO object,然后使用 Office365-REST-Python-Client 将其写入 SharePoint。

另外,他告诉参考上传file.py

我已经按照他的建议进行了更改,这就是我的代码的样子,

import os
from io import BytesIO

from office365.sharepoint.client_context import ClientContext
from tests import test_user_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_user_credentials)

path = "../../data/report #123.xlsx"



buffer = BytesIO()               # Create a buffer object
df.to_excel(buffer, index=False) # Write the dataframe to the buffer
buffer.seek(0)
file_content = buffer.read()



list_title = "Documents"
target_folder = ctx.web.lists.get_by_title(list_title).root_folder
name = os.path.basename(path)
target_file = target_folder.upload_file(name, file_content).execute_query()
print("File has been uploaded to url: {0}".format(target_file.serverRelativeUrl))

但我收到导入错误ImportError: cannot import name 'test_user_credentials' from 'tests'

我不确定我是否缺少任何 package。 我尝试进行pip install tests ,但它不起作用。

我已经安装了 python office 365 rest api 使用pip install Office365-REST-Python-Client

我也尝试在网上查找,但无法找到此测试模块。 我是否缺少 package?任何帮助将不胜感激

from tests import test_site_url, settings

测试模块是私有模块,不需要下载表格 pip 或 pypi,你应该像这样下载所有文件夹

在此处输入图像描述

在此处输入图像描述

你的代码会被找到

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM