Skip to content

BUG: Comment in ODS-file gets included in string cells #55727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ I/O
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)
- Bug in :func:`to_excel`, with ``OdsWriter`` (``ods`` files) writing boolean/string value (:issue:`54994`)
- Bug in :meth:`DataFrame.to_hdf` and :func:`read_hdf` with ``datetime64`` dtypes with non-nanosecond resolution failing to round-trip correctly (:issue:`55622`)
- Bug in :meth:`pandas.read_excel` with ``engine="odf"`` (``ods`` files) when string contains annotation (:issue:`55200`)
- Bug in :meth:`pandas.read_excel` with an ODS file without cached formatted cell for float values (:issue:`55219`)
- Bug where :meth:`DataFrame.to_json` would raise an ``OverflowError`` instead of a ``TypeError`` with unsupported NumPy types (:issue:`55403`)
-
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/excel/_odfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ def _get_cell_string_value(self, cell) -> str:
"""
from odf.element import Element
from odf.namespaces import TEXTNS
from odf.office import Annotation
from odf.text import S

office_annotation = Annotation().qname
text_s = S().qname

value = []
Expand All @@ -239,6 +241,8 @@ def _get_cell_string_value(self, cell) -> str:
if fragment.qname == text_s:
spaces = int(fragment.attributes.get((TEXTNS, "c"), 1))
value.append(" " * spaces)
elif fragment.qname == office_annotation:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cell value is the same as a cell comment, does this still work correctly?

Copy link
Contributor Author

@dimastbk dimastbk Oct 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for files from LibreOffice at least:
image
get_sheet_data output: [['Column 1'], ['test'], [''], ['test 3']]
dataframe:

Column 1
0     test
1      NaN
2   test 3

It is because table:table-cell doesn't contain office:value-type attribute and the type of cell is None, so we don't check office:annotation.

I added a test.

continue
else:
# recursive impl needed in case of nested fragments
# with multiple spaces
Expand Down
Binary file not shown.
11 changes: 11 additions & 0 deletions pandas/tests/io/excel/test_odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,14 @@ def test_read_unempty_cells():
result = pd.read_excel("test_unempty_cells.ods")

tm.assert_frame_equal(result, expected)


def test_read_cell_annotation():
expected = pd.DataFrame(
["test", np.nan, "test 3"],
columns=["Column 1"],
)

result = pd.read_excel("test_cell_annotation.ods")

tm.assert_frame_equal(result, expected)