Coverage for src/artemis_sg/foo.py: 0%

1 import math

2 from copy import copy

3

4 from openpyxl import load_workbook

5 from openpyxl.utils import get_column_letter

6 from openpyxl.worksheet.dimensions import ColumnDimension, DimensionHolder

7

8 from artemis_sg.config import CFG

9

10

11 def get_sheet_keys(ws):

12 for row in ws.values:

13 sheet_keys = [x.upper() if isinstance(x, str) else x for x in row]

14 break

15 return sheet_keys

16

17 def shift_col(ws, col_key, target_idx):

18 ws.insert_cols(target_idx)

19 sheet_keys = get_sheet_keys(ws)

20 sheet_key_idx = sheet_keys.index(col_key) + 1 # for openpyxl

21 sheet_key_idx_ltr = get_column_letter(sheet_key_idx)

22 col_delta = target_idx - sheet_key_idx

23 ws.move_range(f"{sheet_key_idx_ltr}1:{sheet_key_idx_ltr}{ws.max_row}",

24 rows=0, cols=col_delta)

25 ws.delete_cols(sheet_key_idx)

26

27 def copy_cell_style(ws, style_src_cell, target_cell):

28 if style_src_cell.has_style:

29 ws[target_cell].font = copy(style_src_cell.font)

30 ws[target_cell].border = copy(style_src_cell.border)

31 ws[target_cell].fill = copy(style_src_cell.fill)

32 ws[target_cell].number_format = copy(style_src_cell.number_format)

33 ws[target_cell].protection = copy(style_src_cell.protection)

34 ws[target_cell].alignment = copy(style_src_cell.alignment)

35

36 def create_col(ws, col_key, target_idx, style_src_cell=None):

37 ws.insert_cols(target_idx)

38 col_header = f"{get_column_letter(target_idx)}1"

39 ws[col_header] = col_key

40 if style_src_cell:

41 copy_cell_style(ws, style_src_cell, col_header)

42

43 def sequence_worksheet(ws, col_order):

44 sheet_keys = get_sheet_keys(ws)

45 for i, key_name in enumerate(col_order):

46 order_idx = i + 1 # for openpyxl

47 get_column_letter(order_idx)

48 if key_name == "ISBN":

49 key_name = isbn_key # noqa: PLW2901

50 if key_name in sheet_keys:

51 shift_col(ws, key_name, order_idx)

52 else:

53 create_col(ws, key_name, order_idx)

54

55 def size_sheet_cols(ws):

56 dim_holder = DimensionHolder(worksheet=ws)

57 sheet_keys = get_sheet_keys(ws)

58 for i, key_name in enumerate(sheet_keys):

59 col_idx = i + 1 # for openpyxl

60 col_idx_ltr = get_column_letter(col_idx)

61 width = (

62 max(len(str(cell.value)) for cell in ws[col_idx_ltr])

63 * CFG["asg"]["spreadsheet"]["sheet_image"]["col_buffer"]

64 )

65 if width > CFG["asg"]["spreadsheet"]["sheet_image"]["max_col_width"]:

66 width = CFG["asg"]["spreadsheet"]["sheet_image"]["max_col_width"]

67 dim_holder[col_idx_ltr] = ColumnDimension(ws, index=col_idx_ltr, width=width)

68 if key_name == isbn_key:

69 dim_holder[col_idx_ltr] = ColumnDimension(

70 ws,

71 index=col_idx_ltr,

72 width=math.ceil(

73 CFG["asg"]["spreadsheet"]["sheet_image"]["isbn_col_width"]

74 * CFG["asg"]["spreadsheet"]["sheet_image"]["col_buffer"]

75 ),

76 )

77 if key_name == "IMAGE":

78 dim_holder[col_idx_ltr] = ColumnDimension(

79 ws,

80 index=col_idx_ltr,

81 width=CFG["asg"]["spreadsheet"]["sheet_image"]["image_col_width"]

82 )

83

84 ws.column_dimensions = dim_holder

85

86

87 isbn_key = "ISBN-13"

88 col_order = CFG["asg"]["spreadsheet"]["sheet_image"]["col_order"]

89 workbook = "/home/john/Downloads/foo.xlsx"

90

91 wb = load_workbook(workbook)

92 ws = wb.worksheets[0]

93

94 sequence_worksheet(ws, col_order)

95 size_sheet_cols(ws)

96 wb.save(workbook)