我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用xlrd.XL_CELL_EMPTY。
def cell_display(cell, datemode=0, encoding='ascii'): cty = cell.ctype if cty == xlrd.XL_CELL_EMPTY: return 'undefined' if cty == xlrd.XL_CELL_BLANK: return 'blank' if cty == xlrd.XL_CELL_NUMBER: return 'number (%.4f)' % cell.value if cty == xlrd.XL_CELL_DATE: try: return "date (%04d-%02d-%02d %02d:%02d:%02d)" \ % xlrd.xldate_as_tuple(cell.value, datemode) except xlrd.xldate.XLDateError: return "date? (%.6f)" % cell.value if cty == xlrd.XL_CELL_TEXT: return "text (%s)" % cell.value.encode(encoding, 'replace') if cty == xlrd.XL_CELL_ERROR: if cell.value in xlrd.error_text_from_code: return "error (%s)" % xlrd.error_text_from_code[cell.value] return "unknown error code (%r)" % cell.value if cty == xlrd.XL_CELL_BOOLEAN: return "logical (%s)" % ['FALSE', 'TRUE'][cell.value] raise Exception("Unknown Cell.ctype: %r" % cty)
def __is_header_row(self, row_idx): cell_type_list = self._worksheet.row_types( row_idx, self._start_col_idx, self._end_col_idx + 1) return xlrd.XL_CELL_EMPTY not in cell_type_list
def __is_empty_cell_type_list(cell_type_list): return all([ cell_type == xlrd.XL_CELL_EMPTY for cell_type in cell_type_list ])
def show_name_object(book, nobj, show_contents=0, f=sys.stdout): print("\nName: %s, scope: %s (%s)" \ % (REPR(nobj.name), REPR(nobj.scope), scope_as_string(book, nobj.scope)), file=f) res = nobj.result print("Formula eval result: %s" % REPR(res), file=f) 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 range(len(value)): ref3d = value[i] print("Range %d: %s ==> %s"% (i, REPR(ref3d.coords), REPR(xlrd.rangename3drel(book, ref3d))), file=f) elif kind == xlrd.oREF: # A list of Ref3D objects for i in range(len(value)): ref3d = value[i] print("Range %d: %s ==> %s"% (i, REPR(ref3d.coords), REPR(xlrd.rangename3d(book, ref3d))), file=f) if not show_contents: continue datemode = book.datemode for shx in range(ref3d.shtxlo, ref3d.shtxhi): sh = book.sheet_by_index(shx) print(" Sheet #%d (%s)" % (shx, sh.name), file=f) rowlim = min(ref3d.rowxhi, sh.nrows) collim = min(ref3d.colxhi, sh.ncols) for rowx in range(ref3d.rowxlo, rowlim): for colx in range(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(" (%3d,%3d) %-5s: %s" % (rowx, colx, xlrd.cellname(rowx, colx), REPR(sval)), file=f)
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 cell_value(cell, datemode): ctype = cell.ctype value = cell.value if ctype == xlrd.XL_CELL_ERROR: raise ValueError(repr(cell), 'cell error') elif ctype == xlrd.XL_CELL_BOOLEAN: return str(value).upper() elif ctype == xlrd.XL_CELL_NUMBER: if value.is_integer(): value = int(value) return str(value) elif ctype == xlrd.XL_CELL_DATE: value = xlrd.xldate_as_tuple(value, datemode) if value[3:] == (0, 0, 0): return datetime.date(*value[:3]).isoformat() else: return datetime.datetime(*value).isoformat() elif ctype in (xlrd.XL_CELL_TEXT, xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK): return value raise ValueError(repr(cell), 'unknown cell type')
def cell(self,rdrowx,rdcolx,wtrowx,wtcolx): cell = self.rdsheet.cell(rdrowx,rdcolx) # setup column attributes if not already set if wtcolx not in self.wtcols and rdcolx in self.rdsheet.colinfo_map: rdcol = self.rdsheet.colinfo_map[rdcolx] wtcol = self.wtsheet.col(wtcolx) wtcol.width = rdcol.width wtcol.set_style(self.style_list[rdcol.xf_index]) wtcol.hidden = rdcol.hidden wtcol.level = rdcol.outline_level wtcol.collapsed = rdcol.collapsed self.wtcols.add(wtcolx) # copy cell cty = cell.ctype if cty == xlrd.XL_CELL_EMPTY: return if cell.xf_index is not None: style = self.style_list[cell.xf_index] else: style = default_style rdcoords2d = (rdrowx, rdcolx) if rdcoords2d in self.merged_cell_top_left_map: # The cell is the governing cell of a group of # merged cells. rlo, rhi, clo, chi = self.merged_cell_top_left_map[rdcoords2d] assert (rlo, clo) == rdcoords2d self.wtsheet.write_merge( wtrowx, wtrowx + rhi - rlo - 1, wtcolx, wtcolx + chi - clo - 1, cell.value, style) return if rdcoords2d in self.merged_cell_already_set: # The cell is in a group of merged cells. # It has been handled by the write_merge() call above. # We avoid writing a record again because: # (1) It's a waste of CPU time and disk space. # (2) xlwt does not (as at 2007-01-12) ensure that only # the last record is written to the file. # (3) If you write a data record for a cell # followed by a blank record for the same cell, # Excel will display a blank but OOo Calc and # Gnumeric will display the data :-( return wtrow = self.wtsheet.row(wtrowx) if cty == xlrd.XL_CELL_TEXT: wtrow.set_cell_text(wtcolx, cell.value, style) elif cty == xlrd.XL_CELL_NUMBER or cty == xlrd.XL_CELL_DATE: wtrow.set_cell_number(wtcolx, cell.value, style) elif cty == xlrd.XL_CELL_BLANK: wtrow.set_cell_blank(wtcolx, style) elif cty == xlrd.XL_CELL_BOOLEAN: wtrow.set_cell_boolean(wtcolx, cell.value, style) elif cty == xlrd.XL_CELL_ERROR: wtrow.set_cell_error(wtcolx, cell.value, style) else: raise Exception( "Unknown xlrd cell type %r with value %r at (shx=%r,rowx=%r,colx=%r)" \ % (cty, value, sheetx, rowx, colx) )