def test_copy_xlrd(self,c,xlwtw): inwb = object() outwb = Mock() xlwtwi = Mock() xlwtwi.output=[('junk',outwb)] xlwtw.return_value=xlwtwi self.failUnless(copy(inwb) is outwb) self.assertEqual(len(c.call_args_list),1) args = c.call_args_list[0][0] self.assertEqual(len(args),2) r = args[0] self.failUnless(isinstance(r,XLRDReader)) self.failUnless(r.wb is inwb) self.assertEqual(r.filename,'unknown.xls') w = args[1] self.failUnless(w is xlwtwi)
def get_book_sheet(excel_name, sheet_name): """Gets an Excel Worksheet from a given file name Parameters ---------- excel_name : str The full path for the desired Excel file. sheet_name : str The name of the desired Excel Worksheet. Returns ------- workbook, sheet : tuple A tuple with an ``xlwt.Workbook`` and an ``xlwt.Worksheet`` object. """ from desicos.xlrd import open_workbook from desicos.xlutils.copy import copy if os.path.isfile(excel_name): rb = open_workbook(excel_name, formatting_info=True) sheet_names = [s.name for s in rb.sheets()] #rs = rb.sheet_by_index(0) book = copy(rb) sheet = book.get_sheet(0) count = -1 while True: count += 1 new_sheet_name = sheet_name + '_%02d' % count if not new_sheet_name in sheet_names: sheet = book.add_sheet(new_sheet_name) break else: from desicos.xlwt import Workbook book = Workbook() sheet = book.add_sheet(sheet_name + '_00') return book, sheet