Unexpected empty cells returned when reading from an existing workbook loaded as read_only.

Issue #465 resolved
Stuart Filis
created an issue
  • Environment: Python 2.7.9, Windows 7 PC, openpyxl 2.2.3 downloaded from https://pypi.python.org/pypi/openpyxl, reading a .xlsx file.

  • Use Case: I want to load an existing workbook using the "read_only=True" argument to safely open and read values from a workbook without being able to write to it. It is a community owned workbook and the values need to remain the same.

  • Problem: When I use the ws.cell(coordinate).value method on my worksheet object ws to access the data in the cell given by "coordinate", I get None returned. When I use the index notation, ws[coordinate].value, I get the expected data returned.

  • Sample Code: Below is an interactive session using openpyxl to load an example spreadsheet I borrowed from another thread on the Google user board, the spreadsheet file is also attached. In this example I load the same spreadsheet twice, once with the read_only argument and once without, for comparison purposes. It seems that the cell() method on the read_only object (ro_ws) is returning an empty Cell object and the [] notation is returning a ReadOnlyCell object containing the actual value from the spreadsheet.

>>> from openpyxl import load_workbook
>>> ro_wb = load_workbook('Template.xlsx', read_only=True)
>>> wb = load_workbook('Template.xlsx')
>>> ro_wb.get_sheet_names()
['Report']
>>> ro_ws = ro_wb.active
>>> ws = wb.active
>>> ro_ws._cells
{}
>>> ws._cells
{'H1': <Cell Report.H1>}
>>> print ws.cell('H1').value
Hello World
>>> print ws['H1'].value
Hello World
>>> print ro_ws.cell('H1').value
None
>>> print ro_ws['H1'].value
Hello World
>>> print ws.cell('H1')
<Cell Report.H1>
>>> print ws['H1']
<Cell Report.H1>
>>> print ro_ws.cell('H1')
<Cell Report.H1>
>>> print ro_ws['H1']
<openpyxl.cell.read_only.ReadOnlyCell object at 0x0374C7E0>
>>> print ws._cells
{'H1': <Cell Report.H1>}
>>> print ro_ws._cells
{'H1': <Cell Report.H1>}
>>> 

Comments (7)

  1. Log in to comment