Python Forum
Python openyxl not updating Excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python openyxl not updating Excel file
#1
I am at wit's end trying to debug this program. Problem is, it does not update my Excel file. Cry Sad

 
def GetExistingOrders(self, list, sheet):
        # todo define function
        # load from list to sheet
        for row_idx, row_data in enumerate(list, start=2):
            for col_idx, value in enumerate(row_data, start=1):
                sheet.cell(row=row_idx, column=col_idx, value=value)


        sheet['A8'] = 'Test3'

        # self.wb['Extract']
        s1 = self.wb.worksheets[0]
        s1['A11'] = 'Test11'

        s2 = self.wb['data']
        s2.cell(row=6, column=1, value='Test3')

        print(f"Inside s1.A5 = {s1['A5'].value}")
        print(f"Inside s1.A6 = {s1['A6'].value}")
        print(f"Inside sheet.A8 = {sheet['A8'].value}")
        print(f"Inside s1.A9 = {s1['A9'].value}") # I had entered this cell through Excel which prints correctly in python
        print(f"Inside s1.A11 = {s1['A11'].value}") 
        print(f"Inside sheet.A2 = {sheet['A2'].value}") # prints correctly from the above loop
        return
This is how I am calling the function, and the value is also returned from the function and prints outside.

         evar.GetExistingOrders(api.open_orders, sheet)
         print(f"In main: "+str(sheet['A5'].value)) # This also prints correctly outside the function
So far, I have ruled out:

Its pointing to correct Excel. If my rename Excel, it throws an error.
Same for the sheet. I tried renaming sheet and it throws an error.
I have tried debugging and fetching the data and printing it. It correctly prints the data, so the data is there.
I have tried closing the file and Excel application itself, while python program is running.
I have tried updating Excel to random values and fetching them. It works correctly.
So, I have isolated this problem. I can fetch the values. Write to a cell and fetch it in python. But the value does not reflect when I open the Excel file.

Below is how I have opened the file in a setup function.

        self.wb = openpyxl.load_workbook(self.filepath+self.filename)
        sheet = self.wb[sheetname]
Reply
#2
Figured it out. I need to explicitly save the wb. I had seen the save function, but since it mandates a filename, I thought it was only for save as.

I am an idiot. Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating formulas in Excel - best solution MasterOfDestr 4 682 Mar-11-2024, 01:02 PM
Last Post: MasterOfDestr
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,265 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 855 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,116 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,084 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 823 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 860 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,052 Jan-21-2023, 06:57 AM
Last Post: jacklee26
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,134 Dec-15-2022, 04:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020