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

1#!/usr/bin/env python 

2# cardinal_pythonlib/excel.py 

3 

4""" 

5=============================================================================== 

6 

7 Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com). 

8 

9 This file is part of cardinal_pythonlib. 

10 

11 Licensed under the Apache License, Version 2.0 (the "License"); 

12 you may not use this file except in compliance with the License. 

13 You may obtain a copy of the License at 

14 

15 https://www.apache.org/licenses/LICENSE-2.0 

16 

17 Unless required by applicable law or agreed to in writing, software 

18 distributed under the License is distributed on an "AS IS" BASIS, 

19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

20 See the License for the specific language governing permissions and 

21 limitations under the License. 

22 

23=============================================================================== 

24 

25**Functions for dealing with Excel spreadsheets.** 

26 

27""" 

28 

29import datetime 

30import io 

31from typing import Any 

32import uuid 

33 

34from numpy import float64 

35from openpyxl import Workbook 

36from pendulum.datetime import DateTime 

37from semantic_version import Version 

38 

39from cardinal_pythonlib.datetimefunc import pendulum_to_datetime 

40 

41 

42def excel_to_bytes(wb: Workbook) -> bytes: 

43 """ 

44 Obtain a binary version of an :class:`openpyxl.Workbook` representation of 

45 an Excel file. 

46 """ 

47 memfile = io.BytesIO() 

48 wb.save(memfile) 

49 return memfile.getvalue() 

50 

51 

52def convert_for_openpyxl(x: Any) -> Any: 

53 """ 

54 Converts known "unusual" data types to formats suitable for ``openpyxl``. 

55 Specifically, handles: 

56 

57 - :class:`pendulum.datetime.DateTime` 

58 - :class:`semantic_version.Version` 

59 - :class:`uuid.UUID` 

60 

61 Args: 

62 x: a data value 

63 

64 Returns: 

65 the same thing, or a more suitable value! 

66 """ 

67 if isinstance(x, DateTime): 

68 return pendulum_to_datetime(x) 

69 elif isinstance(x, (Version, uuid.UUID)): 

70 return str(x) 

71 else: 

72 return x 

73 

74 

75def convert_for_pyexcel_ods3(x: Any) -> Any: 

76 """ 

77 Converts known "unusual" data types to formats suitable for 

78 ``pyexcel-ods3``. Specifically, handles: 

79 

80 - :class:`pendulum.datetime.DateTime` 

81 - :class:`datetime.datetime` 

82 - :class:`semantic_version.Version` 

83 - ``None`` 

84 - :class:`numpy.float64` 

85 - :class:`uuid.UUID` 

86 

87 Args: 

88 x: a data value 

89 

90 Returns: 

91 the same thing, or a more suitable value! 

92 """ 

93 

94 if isinstance(x, (DateTime, datetime.datetime)): 

95 # ISO 8601, e.g. 2013-07-24T20:04:07+0100) 

96 return x.strftime("%Y-%m-%dT%H:%M:%S%z") 

97 elif x is None: 

98 return "" 

99 elif isinstance(x, (Version, uuid.UUID)): 

100 return str(x) 

101 elif isinstance(x, float64): 

102 return float(x) 

103 else: 

104 return x