Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from typing import List 

2 

3from pandas._typing import FilePathOrBuffer, Scalar 

4from pandas.compat._optional import import_optional_dependency 

5 

6from pandas.io.excel._base import _BaseExcelReader 

7 

8 

9class _PyxlsbReader(_BaseExcelReader): 

10 def __init__(self, filepath_or_buffer: FilePathOrBuffer): 

11 """Reader using pyxlsb engine. 

12 

13 Parameters 

14 __________ 

15 filepath_or_buffer: string, path object, or Workbook 

16 Object to be parsed. 

17 """ 

18 import_optional_dependency("pyxlsb") 

19 # This will call load_workbook on the filepath or buffer 

20 # And set the result to the book-attribute 

21 super().__init__(filepath_or_buffer) 

22 

23 @property 

24 def _workbook_class(self): 

25 from pyxlsb import Workbook 

26 

27 return Workbook 

28 

29 def load_workbook(self, filepath_or_buffer: FilePathOrBuffer): 

30 from pyxlsb import open_workbook 

31 

32 # Todo: hack in buffer capability 

33 # This might need some modifications to the Pyxlsb library 

34 # Actual work for opening it is in xlsbpackage.py, line 20-ish 

35 

36 return open_workbook(filepath_or_buffer) 

37 

38 @property 

39 def sheet_names(self) -> List[str]: 

40 return self.book.sheets 

41 

42 def get_sheet_by_name(self, name: str): 

43 return self.book.get_sheet(name) 

44 

45 def get_sheet_by_index(self, index: int): 

46 # pyxlsb sheets are indexed from 1 onwards 

47 # There's a fix for this in the source, but the pypi package doesn't have it 

48 return self.book.get_sheet(index + 1) 

49 

50 def _convert_cell(self, cell, convert_float: bool) -> Scalar: 

51 # Todo: there is no way to distinguish between floats and datetimes in pyxlsb 

52 # This means that there is no way to read datetime types from an xlsb file yet 

53 if cell.v is None: 

54 return "" # Prevents non-named columns from not showing up as Unnamed: i 

55 if isinstance(cell.v, float) and convert_float: 

56 val = int(cell.v) 

57 if val == cell.v: 

58 return val 

59 else: 

60 return float(cell.v) 

61 

62 return cell.v 

63 

64 def get_sheet_data(self, sheet, convert_float: bool) -> List[List[Scalar]]: 

65 return [ 

66 [self._convert_cell(c, convert_float) for c in r] 

67 for r in sheet.rows(sparse=False) 

68 ]