[英]Manipulate Excel file on Sharepoint Python
我正在尝试打开、读取和使用宏,然后使用 Python 在 Sharepoint 上重新保存一个 Excel 文件。使用 Office 365 REST-Python 客户端我可以打开和阅读但很难看到如何执行 rest。
将不胜感激任何帮助,谢谢!
`ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
response = File.open_binary(ctx, relative_url)
#save data to BytesIO stream
bio = io.BytesIO()
bio.write(response.content)
bio.seek(0) #set file object to start
#read file into pandas dataframe
df = pd.read_excel(bio, sheet_name="Overview")
print(df)
df.at[0,"Unnamed: 1"] = "description"
bio2 = io.BytesIO()
#pip install xlsxwriter
writer = pd.ExcelWriter(bio2)
df.to_excel(writer, sheet_name="Overview")
writer.save()
bio2.seek(0)
df = pd.read_excel(bio2, sheet_name="Overview")
workbook = bio2.read()
response2 = File.save_binary(ctx, relative_url, workbook)
print(response2)`
可以参考以下Python脚本在Sharepoint上保存和读取Excel个文件。
#import all the libraries
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
#target url taken from sharepoint and credentials
url = 'https://company.sharepoint.com/user/folder'
path = '/user/folder/Documents/Target_Excel_File_v4.xlsx'
username = 'Dumby_account@company.com'
password = 'Password!'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
response = File.open_binary(ctx, path)
#save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
#read excel file and each sheet into pandas dataframe
df = pd.read_excel(bytes_file_obj, sheet_name = None)
print(df)
这里有一个类似的 SO 线程问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.