1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
client_id = "client_id"
client_secret = "client_secret"
site_url = "site_url"
path = '/sites/test/General/test.xlsm'
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
import xlwings as xw
import openpyxl
credentials = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(credentials)
target_web = ctx.web
ctx.load(target_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
df = pd.read_excel(bytes_file_obj, sheet_name = 'lolo')
print(df)
# test ecriture
df1 = pd.DataFrame([["AAA", "BBB"]], columns=["Spam", "Egg"])
with pd.ExcelWriter(bytes_file_obj, mode="a", engine="openpyxl",if_sheet_exists="replace") as writer:
df1.to_excel(writer, sheet_name="test")
# test ecriture xlwings
nom = bytes_file_obj
sheet = xw.Book(nom).sheets("lolo")
sheet.range(int(1), int(1)).value = "lalallal" |
Partager