def cell(self,rdrowx,rdcolx,wtrowx,wtcolx): cell = self.rdsheet.cell(rdrowx,rdcolx) if cell.ctype == xlrd.XL_CELL_EMPTY: return if cell.ctype == xlrd.XL_CELL_ERROR: logger.error("Cell %s of sheet %r contains a bad value: %s" % ( xlrd.cellname(rdrowx, rdcolx), quoted_sheet_name(self.rdsheet.name), cell_display(cell,self.rdbook.datemode), )) return BaseWriter.cell(self,rdrowx,rdcolx,wtrowx,wtcolx)
def cell(self, rdrowx, rdcolx, wtrowx, wtcolx): cell = self.rdsheet.cell(rdrowx, rdcolx) if cell.ctype == xlrd.XL_CELL_EMPTY: return if cell.ctype == xlrd.XL_CELL_ERROR: logger.error( "Cell %s of sheet %r contains a bad value: %s" % ( xlrd.cellname(rdrowx, rdcolx), quoted_sheet_name(self.rdsheet.name), cell_display(cell, self.rdbook.datemode), ) ) return BaseWriter.cell(self, rdrowx, rdcolx, wtrowx, wtcolx)
def show_name_object(book, nobj, show_contents=0, f=sys.stdout): print >> f, "\nName: %r, scope: %r (%s)" \ % (nobj.name, nobj.scope, scope_as_string(book, nobj.scope)) res = nobj.result print >> f, "Formula eval result: %r" % res if res is None: return # result should be an instance of the Operand class kind = res.kind value = res.value if kind >= 0: # A scalar, or unknown ... you've seen all there is to see. pass elif kind == xlrd.oREL: # A list of Ref3D objects representing *relative* ranges for i in xrange(len(value)): ref3d = value[i] print >> f, "Range %d: %r ==> %s" % ( i, ref3d.coords, xlrd.rangename3drel(book, ref3d)) elif kind == xlrd.oREF: # A list of Ref3D objects for i in xrange(len(value)): ref3d = value[i] print >> f, "Range %d: %r ==> %s" % (i, ref3d.coords, xlrd.rangename3d(book, ref3d)) if not show_contents: continue datemode = book.datemode for shx in xrange(ref3d.shtxlo, ref3d.shtxhi): sh = book.sheet_by_index(shx) print >> f, " Sheet #%d (%s)" % (shx, sh.name) rowlim = min(ref3d.rowxhi, sh.nrows) collim = min(ref3d.colxhi, sh.ncols) for rowx in xrange(ref3d.rowxlo, rowlim): for colx in xrange(ref3d.colxlo, collim): cty = sh.cell_type(rowx, colx) if cty == xlrd.XL_CELL_EMPTY and show_contents == 1: continue cval = sh.cell_value(rowx, colx) sval = showable_cell_value(cty, cval, datemode) print >> f, " (%3d,%3d) %-5s: %r" \ % (rowx, colx, xlrd.cellname(rowx, colx), sval)
def show_name_object(book, nobj, show_contents=0, f=sys.stdout): print >> f, "\nName: %r, scope: %r (%s)" \ % (nobj.name, nobj.scope, scope_as_string(book, nobj.scope)) res = nobj.result print >> f, "Formula eval result: %r" % res if res is None: return # result should be an instance of the Operand class kind = res.kind value = res.value if kind >= 0: # A scalar, or unknown ... you've seen all there is to see. pass elif kind == xlrd.oREL: # A list of Ref3D objects representing *relative* ranges for i in xrange(len(value)): ref3d = value[i] print >> f, "Range %d: %r ==> %s"% (i, ref3d.coords, xlrd.rangename3drel(book, ref3d)) elif kind == xlrd.oREF: # A list of Ref3D objects for i in xrange(len(value)): ref3d = value[i] print >> f, "Range %d: %r ==> %s"% (i, ref3d.coords, xlrd.rangename3d(book, ref3d)) if not show_contents: continue datemode = book.datemode for shx in xrange(ref3d.shtxlo, ref3d.shtxhi): sh = book.sheet_by_index(shx) print >> f, " Sheet #%d (%s)" % (shx, sh.name) rowlim = min(ref3d.rowxhi, sh.nrows) collim = min(ref3d.colxhi, sh.ncols) for rowx in xrange(ref3d.rowxlo, rowlim): for colx in xrange(ref3d.colxlo, collim): cty = sh.cell_type(rowx, colx) if cty == xlrd.XL_CELL_EMPTY and show_contents == 1: continue cval = sh.cell_value(rowx, colx) sval = showable_cell_value(cty, cval, datemode) print >> f, " (%3d,%3d) %-5s: %r" \ % (rowx, colx, xlrd.cellname(rowx, colx), sval)
def check_file(fname, verbose, do_punc=False, fmt_info=0, encoding='ascii', onesheet=''): print print fname if do_punc: checker = ispunc else: checker = None try: book = open_workbook(fname, formatting_info=fmt_info, on_demand=True) except TypeError: try: book = open_workbook(fname, formatting_info=fmt_info) except TypeError: # this is becoming ridiculous book = open_workbook(fname) totold = totnew = totnotnull = 0 if onesheet is None or onesheet == "": shxrange = range(book.nsheets) else: try: shxrange = [int(onesheet)] except ValueError: shxrange = [book.sheet_names().index(onesheet)] for shx in shxrange: sheet = book.sheet_by_index(shx) ngoodrows = number_of_good_rows(sheet, checker) ngoodcols = number_of_good_cols(sheet, checker, nrows=ngoodrows) oldncells = sheet.nrows * sheet.ncols newncells = ngoodrows * ngoodcols totold += oldncells totnew += newncells nnotnull = 0 sheet_density_pct_s = '' if verbose >= 2: colxrange = range(ngoodcols) for rowx in xrange(ngoodrows): rowtypes = sheet.row_types(rowx) for colx in colxrange: if rowtypes[colx] not in null_cell_types: nnotnull += 1 totnotnull += nnotnull sheet_density_pct = (nnotnull * 100.0) / max(1, newncells) sheet_density_pct_s = "; den = %5.1f%%" % sheet_density_pct if verbose >= 3: # which rows have non_empty cells in the right-most column? lastcolx = sheet.ncols - 1 for rowx in xrange(sheet.nrows): cell = sheet.cell(rowx, lastcolx) if cell.ctype != XL_CELL_EMPTY: print "%s (%d, %d): type %d, value %r" % (cellname( rowx, lastcolx), rowx, lastcolx, cell.ctype, cell.value) if (verbose or ngoodrows != sheet.nrows or ngoodcols != sheet.ncols or (verbose >= 2 and ngoodcells and sheet_density_pct < 90.0)): if oldncells: pctwaste = (1.0 - float(newncells) / oldncells) * 100.0 else: pctwaste = 0.0 shname_enc = safe_encode(sheet.name, encoding) print "sheet #%2d: RxC %5d x %3d => %5d x %3d; %4.1f%% waste%s (%s)" \ % (shx, sheet.nrows, sheet.ncols, ngoodrows, ngoodcols, pctwaste, sheet_density_pct_s, shname_enc) if hasattr(book, 'unload_sheet'): book.unload_sheet(shx) if totold: pctwaste = (1.0 - float(totnew) / totold) * 100.0 else: pctwaste = 0.0 print "%d cells => %d cells; %4.1f%% waste" % (totold, totnew, pctwaste)
def check_file(fname, verbose, do_punc=False, fmt_info=0, encoding='ascii', onesheet=''): print print fname if do_punc: checker = ispunc else: checker = None try: book = open_workbook(fname, formatting_info=fmt_info, on_demand=True) except TypeError: try: book = open_workbook(fname, formatting_info=fmt_info) except TypeError: # this is becoming ridiculous book = open_workbook(fname) totold = totnew = totnotnull = 0 if onesheet is None or onesheet == "": shxrange = range(book.nsheets) else: try: shxrange = [int(onesheet)] except ValueError: shxrange = [book.sheet_names().index(onesheet)] for shx in shxrange: sheet = book.sheet_by_index(shx) ngoodrows = number_of_good_rows(sheet, checker) ngoodcols = number_of_good_cols(sheet, checker, nrows=ngoodrows) oldncells = sheet.nrows * sheet.ncols newncells = ngoodrows * ngoodcols totold += oldncells totnew += newncells nnotnull = 0 sheet_density_pct_s = '' if verbose >= 2: colxrange = range(ngoodcols) for rowx in xrange(ngoodrows): rowtypes = sheet.row_types(rowx) for colx in colxrange: if rowtypes[colx] not in null_cell_types: nnotnull += 1 totnotnull += nnotnull sheet_density_pct = (nnotnull * 100.0) / max(1, newncells) sheet_density_pct_s = "; den = %5.1f%%" % sheet_density_pct if verbose >= 3: # which rows have non_empty cells in the right-most column? lastcolx = sheet.ncols - 1 for rowx in xrange(sheet.nrows): cell = sheet.cell(rowx, lastcolx) if cell.ctype != XL_CELL_EMPTY: print "%s (%d, %d): type %d, value %r" % ( cellname(rowx, lastcolx), rowx, lastcolx, cell.ctype, cell.value) if (verbose or ngoodrows != sheet.nrows or ngoodcols != sheet.ncols or (verbose >= 2 and ngoodcells and sheet_density_pct < 90.0) ): if oldncells: pctwaste = (1.0 - float(newncells) / oldncells) * 100.0 else: pctwaste = 0.0 shname_enc = safe_encode(sheet.name, encoding) print "sheet #%2d: RxC %5d x %3d => %5d x %3d; %4.1f%% waste%s (%s)" \ % (shx, sheet.nrows, sheet.ncols, ngoodrows, ngoodcols, pctwaste, sheet_density_pct_s, shname_enc) if hasattr(book, 'unload_sheet'): book.unload_sheet(shx) if totold: pctwaste = (1.0 - float(totnew) / totold) * 100.0 else: pctwaste = 0.0 print "%d cells => %d cells; %4.1f%% waste" % (totold, totnew, pctwaste)