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# Copyright (c) 2010-2020 openpyxl 

2 

3from openpyxl.compat import safe_string 

4from openpyxl.xml.functions import Element, SubElement, whitespace, XML_NS, REL_NS 

5from openpyxl import LXML 

6from openpyxl.utils.datetime import to_excel, days_to_time 

7from datetime import timedelta 

8 

9 

10def _set_attributes(cell, styled=None): 

11 """ 

12 Set coordinate and datatype 

13 """ 

14 coordinate = cell.coordinate 

15 attrs = {'r': coordinate} 

16 if styled: 

17 attrs['s'] = f"{cell.style_id}" 

18 

19 if cell.data_type == "s": 

20 attrs['t'] = "inlineStr" 

21 elif cell.data_type != 'f': 

22 attrs['t'] = cell.data_type 

23 

24 value = cell._value 

25 

26 if cell.data_type == "d": 

27 if cell.parent.parent.iso_dates: 

28 if isinstance(value, timedelta): 

29 value = days_to_time(value) 

30 value = value.isoformat() 

31 else: 

32 attrs['t'] = "n" 

33 value = to_excel(value, cell.parent.parent.epoch) 

34 

35 if cell.hyperlink: 

36 cell.parent._hyperlinks.append(cell.hyperlink) 

37 

38 return value, attrs 

39 

40 

41def etree_write_cell(xf, worksheet, cell, styled=None): 

42 

43 value, attributes = _set_attributes(cell, styled) 

44 

45 el = Element("c", attributes) 

46 if value is None or value == "": 

47 xf.write(el) 

48 return 

49 

50 if cell.data_type == 'f': 

51 shared_formula = worksheet.formula_attributes.get(cell.coordinate, {}) 

52 formula = SubElement(el, 'f', shared_formula) 

53 if value is not None: 

54 formula.text = value[1:] 

55 value = None 

56 

57 if cell.data_type == 's': 

58 inline_string = SubElement(el, 'is') 

59 text = SubElement(inline_string, 't') 

60 text.text = value 

61 whitespace(text) 

62 

63 

64 else: 

65 cell_content = SubElement(el, 'v') 

66 if value is not None: 

67 cell_content.text = safe_string(value) 

68 

69 xf.write(el) 

70 

71 

72def lxml_write_cell(xf, worksheet, cell, styled=False): 

73 value, attributes = _set_attributes(cell, styled) 

74 

75 if value == '' or value is None: 

76 with xf.element("c", attributes): 

77 return 

78 

79 with xf.element('c', attributes): 

80 if cell.data_type == 'f': 

81 shared_formula = worksheet.formula_attributes.get(cell.coordinate, {}) 

82 with xf.element('f', shared_formula): 

83 if value is not None: 

84 xf.write(value[1:]) 

85 value = None 

86 

87 if cell.data_type == 's': 

88 with xf.element("is"): 

89 attrs = {} 

90 if value != value.strip(): 

91 attrs["{%s}space" % XML_NS] = "preserve" 

92 el = Element("t", attrs) # lxml can't handle xml-ns 

93 el.text = value 

94 xf.write(el) 

95 #with xf.element("t", attrs): 

96 #xf.write(value) 

97 else: 

98 with xf.element("v"): 

99 if value is not None: 

100 xf.write(safe_string(value)) 

101 

102 

103if LXML: 

104 write_cell = lxml_write_cell 

105else: 

106 write_cell = etree_write_cell