def __init__(self, shared_table, tags, name, sheet_id, *args, **kwargs): """ :param shared_table: :type shared_table: :param tags: :type tags: :param name: :type name: :param sheet_id: :type sheet_id: """ super(WorkbookSheet, self).__init__(*args, **kwargs) self.name = name self.sheet_id = sheet_id # Первый элемент: начало вывода по вертикали, второй по горизонтали self.sheet_data = SheetData(self._root, cursor=Cursor(), ns=self.NS, shared_table=shared_table, tags=tags, ) self.drawing, self.comments = self.walk_reletion()
class WorkbookSheet(ReletionOpenXMLFile): u""" Лист книги документа в формате XLSX """ NS = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' NS_R = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' def __init__(self, shared_table, tags, name, sheet_id, *args, **kwargs): """ :param shared_table: :type shared_table: :param tags: :type tags: :param name: :type name: :param sheet_id: :type sheet_id: """ super(WorkbookSheet, self).__init__(*args, **kwargs) self.name = name self.sheet_id = sheet_id # Первый элемент: начало вывода по вертикали, второй по горизонтали self.sheet_data = SheetData(self._root, cursor=Cursor(), ns=self.NS, shared_table=shared_table, tags=tags, ) self.drawing, self.comments = self.walk_reletion() def walk_reletion(self): """ """ drawing = comments = None if not self._reletion_root is None: for elem in self._reletion_root: param = (elem.attrib['Id'], elem.attrib['Target']) if elem.attrib['Type'] == ReletionTypes.DRAWING: drawing = self._get_drawing(*param) elif elem.attrib['Type'] == ReletionTypes.COMMENTS: comments = self._get_comment(*param) return drawing, comments def _get_comment(self, rel_id, target): u""" Получение объекта комментария """ return Comments.create(self.sheet_data, rel_id, *self._get_path(target)) def _get_drawing(self, rel_id, target): u""" Unused """ def __str__(self): res = [u'Sheet name "{0}":'.format(self.name)] if self.comments: for section in self.sections: res.append(u'\t %s' % section) return u'\n'.join(res).encode('utf-8') def __repr__(self): return self.__str__() @property def sections(self): return self.comments.get_sections() def get_section(self, name): u""" Получение секции по имени """ return self.comments.get_section(name) def get_sections(self): u""" Получение всех секций """ return self.sections def build(self): u""" Сборка xml-файла """ new_root = self.sheet_data.new_sheet() with open(self.file_path, 'w') as f: f.write(XML_DEFINITION + tostring(new_root)) if self.comments: self.comments.build() def get_rowbreaks(self): return self.sheet_data.get_rowbreaks() def get_colbreaks(self): return self.sheet_data.get_colbreaks()