Skip to content

file cache? get_sheet_dimension error #27

@mmonacci

Description

@mmonacci

Description

I have prepared a module that contains functions for using Excelize:

Steps to reproduce the issue

import excelize as xl

  1. To handle the file:
    @contextmanager
    def file_manager(file_path: str) -> Generator[xl.File, None]:
    file: File | None = None
    try:
    file = xl.open_file(filename=file_path)
    yield file
    finally:
    if file:
    file.close()

2: It is used by all reading and writing functions, including the one I explain below:

def append_rows(file_path: str, wsh_name: str, data: Matrix) -> bool:
"""Append rows to a sheet"""
try:
with file_manager(file_path=file_path) as f:
next_row_index: int = get_next_unused_row_index(file=f, wsh_name=wsh_name)
for idx, row in enumerate(iterable=data):
cell: str = xl.coordinates_to_cell_name(1, next_row_index + idx, False)
f.set_sheet_row(sheet=wsh_name, cell=cell, values=row)
f.save()
return True
except RuntimeError as err:
print(err)
return False

here:
Matrix: TypeAlias = List[List[Any]]

def get_next_unused_row_index(file: File, wsh_name: str) -> int:
dim: str = file.get_sheet_dimension(sheet=wsh_name)
begin, end = sheet_dimension_begin_end(name=dim) # Just separate A1:F100 at begin = A1, end=F100
col, row = xl.cell_name_to_coordinates(cell=end) # then take r,c from end
return row + 1 # Return r+1

The problem is when I run consecutive tests, "get_sheet_dimension" always returns the same thing.

For example:
def test_append_rows() -> None:

fn = r"C:\Py\proj\files\V.xlsx"
wsh_name = "L"
data: Matrix = [["UPDATE", "MM", datetime.now(), "Mock", "mock", "name", "M", "A"]]
print(append_rows(file_path=fn, wsh_name=wsh_name, data=data))	

However, the file is saved every time.

But, if between runs
I open the file with Excel and save it
Then, "get_sheet_dimension" returns the correct value (...so append works fine)

The question is:
Did "Excelize" cache the file somehow?

(In my tests, the id(file) is different in each run)

Describe the results you received

get_sheet_dimension = original range, before the appends

Describe the results you expected

get_sheet_dimension = current, after appends

Python version

3.13.5

Excelize version or commit ID

0.0.4

Environment

Windows 11 Home (64 bits)
Microsoft Office Professional Plus 2019, 32 bits (We still use VB6 dll referenced in VBA)

Validations

  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions