Bonjour à tous
J'utilise ce code pour lire un fichier Excel qui se trouve sous SharePoint, pour le lire, aucun problème le code marche très bien cependant, je souhaiterais aussi le modifier et là je bloque un peu, pouvez-vous m'aider ? sous le forum Python on me dit d’utiliser le module O365.Excel
Je me retrouve avec l’erreur :
1 2
| raise ValueError('This file is not a valid Excel xlsx file.')
ValueError: This file is not a valid Excel xlsx file. |
a vrai dire je sais pas vraiment quoi mettre dans la partie :
excel_file = WorkBook(response) # my_file_instance should be an instance of File.
suivant le conseil du forum Python, je poste ici pour trouver une solution.
merci pour votre aide
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
| client_id = "client_id "
client_secret = "client_secret"
site_url = "site_url"
path = '/sites/Macrotest/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
from O365.excel import WorkBook
credentials = ClientCredential(client_id, client_secret)
ctx = ClientContext(site_url).with_credentials(credentials)
target_web = ctx.web
ctx.load(target_web)
ctx.execute_query()
web = ctx.web.get().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
# test ecritur
# given a File instance that is a xlsx file ...
print(response)
excel_file = WorkBook(response) # my_file_instance should be an instance of File.
ws = excel_file.get_worksheet('lolo')
cella1 = ws.get_range('A1')
cella1.values = 35
cella1.update() |
Partager