Python Forum
Updating sharepoint excel file odd results - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Updating sharepoint excel file odd results (/thread-41042.html)



Updating sharepoint excel file odd results - cubangt - Nov-01-2023

So we display a excel file on our tv's in the office that has a embedded image on the sheet. I have a python script that updates that image daily. When this was implemented earlier this year, it worked like a charm and now noticed and was informed that it wasnt updating. So here is what im noticing and not sure how to change the code to help deal with this issue..

Script runs no errors or issues and when you look at the timestamp of the file, it shows it was modified at the scheduled time. When you open the file locally you see the updated image, BUT you also see a message at the top of the file that says there is a newer version. If you click on it and say you want to see the latest version, the updated image is removed and you see the image that was there before the update.

And because of this, the TV display never shows the updated image unless i open the file and reload the image manually.

#1 Is this common with sharepoint files?
#2 Is there a way to incorporate a delay between the update and the save to see if maybe its a timing issue?

I have noticed that on other files that we open from our teams server, if you make a change, you cant save it right away.. the "autosave" feature can take 2sec up to 10 sec to save. (this is in reference to manually opening and modifying files, not python)

Since i have noticed that when manually editing files, im wondering if that is the problem with trying to update via the script below.

import win32com.client
from pathlib import Path
from datetime import datetime, timedelta
import openpyxl

PricingVolumeFile = 'C:\\Users\\PricingVolumes2022.xlsx'

outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
    
inbox = mapi.GetDefaultFolder(6)

inbox = mapi.GetDefaultFolder(6).Folders["Notifications"].Folders["Pricing"].Folders["Peak Future Volume"]

# THIS IS USED TO GRAB THE MESSAGE DATE SO IT CAN PROCESS ONLY THE LATEST EMAILS (EXAMPLE BELOW IS LAST 1 DAY)
received_dt = datetime.now() - timedelta(days=1)
received_dt = received_dt.strftime('%m/%d/%Y %H:%M %p')

edateFileSaveDate = datetime.now()
edateFileSaveDate = edateFileSaveDate.strftime('%Y%m%d')

messages = inbox.Items
messages = messages.Restrict("[ReceivedTime] >= '" + received_dt + "'")

for message in messages: 
    #     # THIS IS USED TO READ THE SUBJECT AND PARSE OUT THE DATE FOR THE APPROPRIATE MESSAGES
    sub = message.subject
    # TRY USING THIS ACTUAL MESSAGE DATE FOR THE IF CONDITION BELOW
    messD = message.ReceivedTime
    
    edDate = sub.split()[5]
    edreceived_dt = datetime.now()
    edreceived_dt = edreceived_dt.strftime('%m/%d/%Y %H:%M %p')
    tdDate = edreceived_dt.split()[0]      
    
    # CHECK IF MESSAGE IS TODAY SO THAT THE MOST CURRENT SCREENSHOT IS UPDATED IN THE FILE.
    if edDate == tdDate:
        attachments = message.Attachments
        # THIS ENSURES THAT THE EMAIL HAS AT LEAST 2 ATTACHMENTS BEFORE IT TRIES TO DOWNLOAD FILES THAT DONT EXIST    
        if attachments.count >= 1:
            
            #LOOP THRU ALL ATTACHMENTS AND ONLY DOWNLOAD CSV FILES
            for att in attachments:
               p = Path(str(att).lower())
               
               #newName = "{0}_{1}".format(p.stem,p.suffix)
               newName = edateFileSaveDate + "{0}".format(p.suffix)
               
               # CHECK IF ITS A PNG FILE BEFORE ATTEMPTING TO DOWNLOAD
               if p.suffix == ".png":
                  imgFileLoc = "C:\PricingVolume"+ '\\' + newName
                  # DOWNLOAD CSV FILE ONLY WITH THE NEW FILE NAME
                  figSaved  = att.SaveASFile(imgFileLoc)
                  #print(att)
                  # THIS IS THE MASTER EXCEL FILE THAT IS UPDATED AT THE END WITH THE FILTERED DATA
                  wrkb = openpyxl.Workbook()
                  ws = wrkb.worksheets[0]
                  img = openpyxl.drawing.image.Image(imgFileLoc)
                  img.height = 757
                  img.width= 1533
                  img.anchor = 'A1'
                  ws.add_image(img)
                  wrkb.save(PeakPricingVolumeFile)
    
    message.Unread = False
    message.Save()



RE: Updating sharepoint excel file odd results - noisefloor - Nov-03-2023

I think this is not a Python problem at all.

When we ran as MS Sharepoint back in the days, we could set permissions and workflows on a per-directory / folder base. E.g. anybody could upload a new revision of the file, but the file won't become visible to other user in the new revision unless somebody with the necessary rights approved the newer version (aka: made it public, so to speak). However, the person who uploaded the new revision always saw the new revision, independent of the appoval status. As far as I remember this was pretty much configurable up to complex workflows for the approval process.

Regards, noisefloor