File getting corrupted during write

Issue #1256 resolved
Marko Bokic created an issue

Excel is getting corrupted everytime i try writing to it. Script code, shortened for brevity rest is just some file copy logic:

import os
import datetime
import openpyxl
import shutil
import re

wb_path = 'E:\\RS All File List_10_02_2017.xlsx'
wb_sheet_name = 'test'

wb = openpyxl.load_workbook(wb_path)
sheet = wb[wb_sheet_name]


i = 0
for row in range(2, sheet.max_row + 1):
    if i == 10:
        i = 0
    i += 1"Results.xlsx")
print("Done :)")

I have tried writing it to separate file or to same file it is generating corrupted file (attached) Just to note that same code worked with 2.5.x version

Comments (9)

  1. CharlieC

    Are you saving the file within the loop? It looks like this is running so fast that different writes to the same file are happening at the same time.

  2. Marko Bokic reporter

    Yes it is within the loop, but usually set to write every 5000 iterations. If i set to save to different location ("Result.xlsx") it will trigger this error message when i interupt it (it generates the same file as attached after first save): PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ...

  3. CharlieC

    I've no idea where you would want to save the same file while still writing to it but the file supplied is not a correct zipfile so that's probably where the problem is, which is what the error message seems to suggest.

    If you stop doing this, the problem probably won't occur.

  4. Marko Bokic reporter

    Edited code to save to new Excel ("Results.xlsx") Still having the same issue. I will try testing it in debug mode.

  5. CharlieC

    The code still expects to write to the same file repeatedly. This still makes no sense to me. If you were to create different files using your counter I suspect the problem will go away. But basically I can't see that this has much to do with openpyxl.

  6. Log in to comment