Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Python openyxl not updating Excel file

$
0
0

I am trying to debug this program. The problem is, it does not update my Excel file.

    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:

  1. Its pointing to correct Excel. If my rename Excel, it throws an error.
  2. Same for the sheet. I tried renaming sheet and it throws an error.
  3. I have tried debugging and fetching the data and printing it. It correctly prints the data, so the data is there.
  4. I have tried closing the file and Excel application itself, while python program is running.
  5. 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]

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>