windows file still locked after Workbook.close() when read_only=True and partial iteration over rows in a Worksheet
Created originally on Bitbucket by DanielM
Below is a minimal example to reproduce (tested on Windows 10 with python 3.7 and openpyxl 3.0.3):
from openpyxl import load_workbook filepath = r'o:\job order list\test.xlsx' print("running test") book = load_workbook(filename=filepath, data_only=True, read_only=True sheet = book["Form QC-300"] for row in sheet.rows: break book.close() print("press enter to continue") x = input()
The file doesn’t get unlocked until the program exits. Expected result is that it gets unlocked after book.close() is called.
I believe this is happening because in ‘''_cells_by_row’'' in https://bitbucket.org/openpyxl/openpyxl/src/3.0.3/openpyxl/worksheet/_read_only.py, src.close() is only getting called if you iterate thru all rows.
The file lock gets released as expected if you iterate through all rows. For example, in the code above, replace ‘''break''' with ‘''pass’''.
If read_only=False, the code works as expected.