Skip to content
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
Binary file added tests/test_comments_excel.xlsx
Binary file not shown.
Binary file added tests/test_comments_gdocs.xlsx
Binary file not shown.
46 changes: 46 additions & 0 deletions tests/test_xlsx_comments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from unittest import TestCase

import os

from xlrd import open_workbook

from .base import from_this_dir

class TestXlsxComments(TestCase):

def test_excel_comments(self):
book = open_workbook(from_this_dir('test_comments_excel.xlsx'))
sheet = book.sheet_by_index(0)

note_map = sheet.cell_note_map
self.assertEqual(len(note_map), 1)
self.assertEqual(note_map[(0, 1)].text, 'hello')

def test_excel_comments_multiline(self):
book = open_workbook(from_this_dir('test_comments_excel.xlsx'))
sheet = book.sheet_by_index(1)

note_map = sheet.cell_note_map
self.assertEqual(note_map[(1, 2)].text, '1st line\n2nd line')

def test_excel_comments_two_t_elements(self):
book = open_workbook(from_this_dir('test_comments_excel.xlsx'))
sheet = book.sheet_by_index(2)

note_map = sheet.cell_note_map
self.assertEqual(note_map[(0, 0)].text, 'Author:\nTwo t elements')

def test_excel_comments_no_t_elements(self):
book = open_workbook(from_this_dir('test_comments_excel.xlsx'))
sheet = book.sheet_by_index(3)

note_map = sheet.cell_note_map
self.assertEqual(note_map[(0,0)].text, '')

def test_gdocs_comments(self):
book = open_workbook(from_this_dir('test_comments_gdocs.xlsx'))
sheet = book.sheet_by_index(0)

note_map = sheet.cell_note_map
self.assertEqual(len(note_map), 1)
self.assertEqual(note_map[(0, 1)].text, 'Just a test')
32 changes: 31 additions & 1 deletion xlrd/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,31 @@ def own_process_stream(self, stream, heading=None):
elif elem.tag == U_SSML12 + "dimension":
self.do_dimension(elem)
self.finish_off()


def process_comments_stream(self, stream):
root = ET.parse(stream).getroot()
author_list = root[0]
assert author_list.tag == U_SSML12 + 'authors'
authors = [elem.text for elem in author_list]
comment_list = root[1]
assert comment_list.tag == U_SSML12 + 'commentList'
cell_note_map = self.sheet.cell_note_map
from .sheet import Note
text_tag = U_SSML12 + 'text'
r_tag = U_SSML12 + 'r'
t_tag = U_SSML12 + 't'
for elem in comment_list.findall(U_SSML12 + 'comment'):
ts = elem.findall('./' + text_tag + '/' + t_tag)
ts += elem.findall('./' + text_tag + '/' + r_tag + '/' + t_tag)
ref = elem.get('ref')
note = Note()
note.author = authors[int(elem.get('authorId'))]
note.rowx, note.colx = coords = cell_name_to_rowx_colx(ref)
note.text = ''
for t in ts:
note.text += cooked_text(self, t)
cell_note_map[coords] = note

def do_dimension(self, elem):
ref = elem.get('ref') # example: "A1:Z99" or just "A1"
if ref:
Expand Down Expand Up @@ -762,6 +786,12 @@ def open_workbook_2007_xml(
heading = "Sheet %r (sheetx=%d) from %r" % (sheet.name, sheetx, fname)
x12sheet.process_stream(zflo, heading)
del zflo
comments_fname = 'xl/comments%d.xml' % (sheetx + 1)
if comments_fname in component_names:
comments_stream = getzflo(zf, comments_fname)
x12sheet.process_comments_stream(comments_stream)
del comments_stream

sheet.tidy_dimensions()

return bk