Skip to content

Commit

Permalink
Temporarily fix openpyxl read_worksheets issue
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Feb 29, 2024
1 parent 194572b commit 8f7aa0b
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions Orange/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# This module is a mixture of imports and code, so we allow import anywhere
# pylint: disable=wrong-import-position,wrong-import-order
import warnings

from openpyxl.cell import MergedCell
from openpyxl.comments.comment_sheet import CommentSheet
from openpyxl.drawing.spreadsheet_drawing import SpreadsheetDrawing
from openpyxl.packaging.relationship import get_rels_path, RelationshipList, \
get_dependents
from openpyxl.pivot.table import TableDefinition
from openpyxl.reader.drawings import find_images
from openpyxl.reader.excel import ExcelReader
from openpyxl.worksheet._read_only import ReadOnlyWorksheet
from openpyxl.worksheet._reader import WorksheetReader
from openpyxl.xml.constants import COMMENTS_NS
from openpyxl.xml.functions import fromstring
from openpyxl.worksheet.table import Table

from Orange import data

Expand Down Expand Up @@ -46,3 +61,82 @@
pass
finally:
del ctypes


# temporary fix for file not closed issue until openpyxl prepare release
# https://foss.heptapod.net/openpyxl/openpyxl/-/merge_requests/436#7922bd5f66e11e4ca4539f093b2680a25c1f80db
def read_worksheets(self):
comment_warning = """Cell '{0}':{1} is part of a merged range but has a comment which will be removed because merged cells cannot contain any data."""
for sheet, rel in self.parser.find_sheets():
if rel.target not in self.valid_files:
continue

Check warning on line 72 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L72

Added line #L72 was not covered by tests

if "chartsheet" in rel.Type:
self.read_chartsheet(sheet, rel)
continue

Check warning on line 76 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L75-L76

Added lines #L75 - L76 were not covered by tests

rels_path = get_rels_path(rel.target)
rels = RelationshipList()
if rels_path in self.valid_files:
rels = get_dependents(self.archive, rels_path)

if self.read_only:
ws = ReadOnlyWorksheet(self.wb, sheet.name, rel.target, self.shared_strings)
ws.sheet_state = sheet.state
self.wb._sheets.append(ws)
continue

Check warning on line 87 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L84-L87

Added lines #L84 - L87 were not covered by tests
else:
fh = self.archive.open(rel.target)
ws = self.wb.create_sheet(sheet.name)
ws._rels = rels
ws_parser = WorksheetReader(ws, fh, self.shared_strings, self.data_only, self.rich_text)
ws_parser.bind_all()
fh.close()

# assign any comments to cells
for r in rels.find(COMMENTS_NS):
src = self.archive.read(r.target)
comment_sheet = CommentSheet.from_tree(fromstring(src))
for ref, comment in comment_sheet.comments:
try:
ws[ref].comment = comment
except AttributeError:
c = ws[ref]
if isinstance(c, MergedCell):
warnings.warn(comment_warning.format(ws.title, c.coordinate))
continue

Check warning on line 107 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L98-L107

Added lines #L98 - L107 were not covered by tests

# preserve link to VML file if VBA
if self.wb.vba_archive and ws.legacy_drawing:
ws.legacy_drawing = rels.get(ws.legacy_drawing).target

Check warning on line 111 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L111

Added line #L111 was not covered by tests
else:
ws.legacy_drawing = None

for t in ws_parser.tables:
src = self.archive.read(t)
xml = fromstring(src)
table = Table.from_tree(xml)
ws.add_table(table)

Check warning on line 119 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L116-L119

Added lines #L116 - L119 were not covered by tests

drawings = rels.find(SpreadsheetDrawing._rel_type)
for rel in drawings:
charts, images = find_images(self.archive, rel.target)
for c in charts:
ws.add_chart(c, c.anchor)

Check warning on line 125 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L125

Added line #L125 was not covered by tests
for im in images:
ws.add_image(im, im.anchor)

Check warning on line 127 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L127

Added line #L127 was not covered by tests

pivot_rel = rels.find(TableDefinition.rel_type)
pivot_caches = self.parser.pivot_caches
for r in pivot_rel:
pivot_path = r.Target
src = self.archive.read(pivot_path)
tree = fromstring(src)
pivot = TableDefinition.from_tree(tree)
pivot.cache = pivot_caches[pivot.cacheId]
ws.add_pivot(pivot)

Check warning on line 137 in Orange/__init__.py

View check run for this annotation

Codecov / codecov/patch

Orange/__init__.py#L132-L137

Added lines #L132 - L137 were not covered by tests

ws.sheet_state = sheet.state


ExcelReader.read_worksheets = read_worksheets

0 comments on commit 8f7aa0b

Please sign in to comment.