from office365.runtime.auth.authentication_context import AuthenticationContext from office365.sharepoint.client_context import ClientContext # SharePoint站点配置 site_url = "https://renesasgroup.sharepoint.com/sites/msteams_cd20b2" doc_library = "Documents" target_folder = "General" # 认证信息 username = "[email protected]" password = "RSB$(zyg707)" def list_general_simple(): """ 简单列出General文件夹内容 """ try: ctx_auth = AuthenticationContext(site_url) if ctx_auth.acquire_token_for_user(username, password): ctx = ClientContext(site_url, ctx_auth) # 获取General文件夹 general_folder = ctx.web.lists.get_by_title(doc_library).root_folder.folders.get_by_url(target_folder).folders.get_by_url('制技部重点课题-周进度管理') ctx.load(general_folder) ctx.execute_query() print(f"General文件夹内容:") print("=" * 50) # 文件 files = general_folder.files ctx.load(files) ctx.execute_query() print("\n文件:") for file in files: print(f" 📄 {file.properties['Name']}") # 文件夹 folders = general_folder.folders ctx.load(folders) ctx.execute_query() print("\n文件夹:") for folder in folders: print(f" 📁 {folder.properties['Name']}/") return True except Exception as e: print(f"错误: {str(e)}") return False # 运行 if __name__ == "__main__": list_general_simple() 制技部周别业务管理表827.xlsx 文件就在最后这个文件夹中,如何获取为dataframe
时间: 2025-09-07 13:47:12 AIGC 浏览: 11
在从 SharePoint 文件夹中读取 Excel 文件并将其转换为 Pandas DataFrame 的过程中,可以借助 `office365-python-client` 库与 SharePoint 进行交互,并使用 `pandas` 读取 Excel 数据。以下是详细步骤和代码示例。
### 从 SharePoint 读取 Excel 文件并转换为 Pandas DataFrame
#### 1. 安装必要的库
确保以下 Python 库已安装:
- `office365-python-client`:用于与 SharePoint 交互
- `pandas`:用于数据处理
- `openpyxl`:用于读取 `.xlsx` 文件
```bash
pip install office365-python-client pandas openpyxl
```
#### 2. 认证并连接到 SharePoint
使用用户名和密码或 OAuth 进行认证,并连接到 SharePoint 站点。以下示例使用用户名和密码进行认证:
```python
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import io
import pandas as pd
# SharePoint 站点信息
site_url = "https://your-sharepoint-site-url"
username = "your-username"
password = "your-password"
# 创建认证上下文
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(site_url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful - Connected to SharePoint site: {0}".format(web.properties['Title']))
else:
print("Authentication failed")
```
#### 3. 下载 SharePoint 中的 Excel 文件
通过文件路径获取文件内容,并将其作为二进制流读取:
```python
file_url = "/sites/your-site/Shared Documents/your-file.xlsx"
# 获取文件内容
file = ctx.web.get_file_by_server_relative_url(file_url)
file_content = file.get_content().execute_query()
# 将文件内容转换为 BytesIO 对象
file_stream = io.BytesIO(file_content.value)
```
#### 4. 使用 Pandas 读取 Excel 数据
使用 `pandas.read_excel()` 函数从内存中的文件流加载数据:
```python
# 使用 Pandas 读取 Excel 数据
df = pd.read_excel(file_stream)
# 显示前几行数据
print(df.head())
```
### 完整代码示例
```python
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import io
import pandas as pd
# SharePoint 站点信息
site_url = "https://your-sharepoint-site-url"
username = "your-username"
password = "your-password"
file_url = "/sites/your-site/Shared Documents/your-file.xlsx"
# 创建认证上下文
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(site_url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful - Connected to SharePoint site: {0}".format(web.properties['Title']))
# 获取文件内容
file = ctx.web.get_file_by_server_relative_url(file_url)
file_content = file.get_content().execute_query()
# 将文件内容转换为 BytesIO 对象
file_stream = io.BytesIO(file_content.value)
# 使用 Pandas 读取 Excel 数据
df = pd.read_excel(file_stream)
# 显示前几行数据
print(df.head())
else:
print("Authentication failed")
```
---
阅读全文
相关推荐















