def copy(wb): w = XLWTWriter() process( XLRDReader(wb, 'unknown.xls'), w ) return w.output[0][1], w.style_list
def copy2(wb): w = XLWTWriter() process( XLRDReader(wb,'unknown.xls'), w ) return w.style_list
def copy_hack(self): """ хак для копирования стилей из шаблона в отчет """ w = XLWTWriter() process(XLRDReader(self.work_book, 'unknown.xls'), w) return w.output[0][1], w.style_list
def copy2(wb): """ Patch: add this function to the end of xlutils/copy.py :param wb: excel workbook using xlrd :return: workbook with format preserved """ w = XLWTWriter() process(XLRDReader(wb, "unknown.xls"), w) return w.output[0][1], w.style_list
def copy(wb): """ Copy an :class:`xlrd.Book` into an :class:`xlwt.Workbook` preserving as much information from the source object as possible. See the :doc:`copy` documentation for an example. """ w = XLWTWriter() process(XLRDReader(wb, 'unknown.xls'), w) return w.output[0][1]
def create_bfx_reimport_xlbook(book, feature_sheetidxs): assert all([f == feature_sheetidxs[0] for f in feature_sheetidxs]), \ "All raw features must be on the same sheet for BFX format" normalization_sheet_name = 'Normalization 1' provenance_sheet_name = 'BFX Provenance Tracking' from xlutils.filter import process, XLRDReader, XLWTWriter, BaseFilter import xlwt class OnlyRelevantSheet(BaseFilter): def __init__(self): self.__on = False def sheet(self, rdsheet, wtsheet_name): if rdsheet == book.sheet_by_index(feature_sheetidxs[0]): self.__on = True # only write this sheet self.next.sheet(rdsheet, wtsheet_name) else: self.__on = False # no call def set_rdsheet(self,rdsheet): self.next.sheet(rdsheet,wtsheet_name) def row(self,rdrowx,wtrowx): if self.__on: self.next.row(rdrowx,wtrowx) def cell(self,rdrowx,rdcolx,wtrowx,wtcolx): if self.__on: self.next.cell(rdrowx,rdcolx,wtrowx,wtcolx) class WrapWT(XLWTWriter): def sheet(self, rdsheet, wtsheet_name): XLWTWriter.sheet(self, rdsheet, wtsheet_name) def finish(self): # add two new sheets at the end self.normalization_sheet = self.wtbook.add_sheet(normalization_sheet_name) self.provenance_sheet = self.wtbook.add_sheet(provenance_sheet_name) XLWTWriter.finish(self) # hackity hack - need to force non-ascii encoding orig_Workbook_init = xlwt.Workbook.__init__ def replacement(self, encoding='utf-8', style_compression=0): orig_Workbook_init(self, encoding, style_compression) xlwt.Workbook.__init__ = replacement w = WrapWT() process(XLRDReader(book, 'unknown.xls'), OnlyRelevantSheet(), w) outbook = w.output[0][1] xlwt.Workbook.__init__ = orig_Workbook_init return outbook, w.normalization_sheet, w.provenance_sheet
def test_setup(self): class DummyReader: def __call__(self,filter): filter.finished() F1 = Mock() F2 = Mock() process(DummyReader(),F1,F2) self.failUnless(F1.next is F2) self.failUnless(isinstance(F2.next,Mock)) compare(F1.method_calls,[('finished',(),{})]) compare(F2.method_calls,[])
def copy(wb): """ Copy an :class:`xlrd.Book` into an :class:`xlwt.Workbook` preserving as much information from the source object as possible. See the :doc:`copy` documentation for an example. """ w = XLWTWriter() process( XLRDReader(wb,'unknown.xls'), w ) return w.output[0][1]
def save(wb, filename_or_stream): "Save the supplied :class:`xlrd.Book` to the supplied stream or filename." if isinstance(filename_or_stream, basestring): filename = os.path.split(filename_or_stream)[1] stream = open(filename_or_stream, "wb") close = True else: filename = "unknown.xls" stream = filename_or_stream close = False process(XLRDReader(wb, filename), StreamWriter(stream)) if close: stream.close()
def save(wb,filename_or_stream): if isinstance(filename_or_stream,basestring): filename = os.path.split(filename_or_stream)[1] stream = open(filename_or_stream,'wb') close = True else: filename = 'unknown.xls' stream = filename_or_stream close = False process( XLRDReader(wb,filename), StreamWriter(stream) ) if close: stream.close()
def test_open_workbook_args(self): r = TestReader(('Sheet1',[['X']])) f = ErrorFilter() m = Mock() process(r,f,m) compare(m.method_calls,[ ('start',(),{}), ('workbook',(C('xlrd.Book', pickleable=0, formatting_info=1, on_demand=False, strict=False),'test.xls'),{}), ('sheet',(C('xlrd.sheet.Sheet'),u'Sheet1'),{}), ('row',(0,0),{}), ('cell',(0,0,0,0),{}), ('finish',(),{}), ])
def test_no_error_on_bools(self,h): r = TestReader( ('Sheet',[[(XL_CELL_BOOLEAN,True)]]), ) book = tuple(r.get_workbooks())[0][0] # fire methods on filter f = ErrorFilter() c = Mock() process(r,f,c) compare(c.method_calls,[ ('start', (), {}), ('workbook', (C('xlrd.Book'), 'test.xls'),{}), ('sheet', (C('xlrd.sheet.Sheet',name='Sheet',strict=False), u'Sheet'),{}), ('row', (0, 0),{}), ('cell', (0, 0, 0, 0),{}), ('finish', (), {}) ]) self.assertEqual(len(h.records),0)
def duplicate_xlbook(book): # add two sheets, one for normalization results, one for provenance tracking existing_sheets = book.sheet_names() count = 1 while 'Normalization %d'%(count) in existing_sheets: count += 1 normalization_sheet_name = 'Normalization %d'%(count) provenance_sheet_name = 'BFX Provenance Tracking' add_provenance_sheet = (provenance_sheet_name not in existing_sheets) from xlutils.filter import process, XLRDReader, XLWTWriter import xlwt class WrapWT(XLWTWriter): def sheet(self, rdsheet, wtsheet_name): # if we're just adding a normalization sheet, add it before the provenance sheet if (not add_provenance_sheet) and (wtsheet_name == provenance_sheet_name): self.normalization_sheet = self.wtbook.add_sheet(normalization_sheet_name) # write the sheet as requested XLWTWriter.sheet(self, rdsheet, wtsheet_name) def finish(self): # add two new sheets at the end if we're adding normalization and results if add_provenance_sheet: self.normalization_sheet = self.wtbook.add_sheet(normalization_sheet_name) self.wtbook.add_sheet(provenance_sheet_name) XLWTWriter.finish(self) # hackity hack - need to force non-ascii encoding orig_Workbook_init = xlwt.Workbook.__init__ def replacement(self, encoding='utf-8', style_compression=0): orig_Workbook_init(self, encoding, style_compression) xlwt.Workbook.__init__ = replacement w = WrapWT() process(XLRDReader(book, 'unknown.xls'), w) outbook = w.output[0][1] xlwt.Workbook.__init__ = orig_Workbook_init for provenance_sheet_idx in range(len(existing_sheets) + 2): if outbook.get_sheet(provenance_sheet_idx).name == provenance_sheet_name: break return w.output[0][1], w.normalization_sheet, outbook.get_sheet(provenance_sheet_idx)
goodlist = None def __init__(self): self.wtrowx = -1 self.skipped_row = -1 def workbook(self, rdbook, wtbook_name): self.next.workbook(rdbook, 'filtered_' + wtbook_name) def row(self, rdrowx, rwrowx): value = self.rdsheet.cell(rdrowx, ROW).value if value == VALUE: self.skipped_row = rdrowx self.wtrowx += 1 def cell(self, rdrowx, rdcolx, wtrowx, wtcolx): if self.skipped_row == rdrowx: self.next.cell(rdrowx, rdcolx, self.wtrowx, wtcolx) xls_file = 's.xls' results_folder = '' VALUE = 'XXX' ROW = 1 process( GlobReader(xls_file), ValueRowFilter(), DirectoryWriter(results_folder) )
def copy(wb): w = XLWTWriter() process(XLRDReader(wb, "unknown.xls"), w) return w.output[0][1]
def run(infile, outfile, filter): wb = xlrd.open_workbook(file_contents=infile.read()) reader = XLRDReader(wb, infile.name) writer = StreamWriter(outfile) process(reader, filter, Anonymize(), writer)
def get_stream(self,filename): return file(filename,'wb') class Filter(BaseFilter): pending_row = None wtrowxi = 0 def workbook(self,rdbook,wtbook_name): self.next.workbook(rdbook,'filtered-'+wtbook_name) def row(self,rdrowx,wtrowx): self.pending_row = (rdrowx,wtrowx) def cell(self,rdrowx,rdcolx,wtrowx,wtcolx): if rdcolx==0: value = self.rdsheet.cell(rdrowx,rdcolx).value if value.strip().lower()=='x': self.ignore_row = True self.wtrowxi -= 1 else: self.ignore_row = False rdrowx, wtrowx = self.pending_row self.next.row(rdrowx,wtrowx+self.wtrowxi) elif not self.ignore_row: self.next.cell( rdrowx,rdcolx,wtrowx+self.wtrowxi,wtcolx-1 ) process(Reader(),Filter(),Writer())