2********************************************************************************
4@brief Util function for openpyxl
5********************************************************************************
8from typing
import Optional
9from datetime
import datetime, time
10from openpyxl
import Workbook
11from openpyxl.utils
import get_column_letter
12from openpyxl.worksheet
import table
13from openpyxl.worksheet.worksheet
import Worksheet
14from openpyxl.styles
import Font, PatternFill, Alignment
15from openpyxl.styles.borders
import Border, Side
29COLOR_ORANGE =
"FFA500"
31COLOR_VIOLET =
"EE82EE"
33COLOR_YELLOW =
"FFFF00"
35THIN_BORDER = Border(left=Side(style=
"thin"),
36 right=Side(style=
"thin"),
37 top=Side(style=
"thin"),
38 bottom=Side(style=
"thin"))
40NUMBER_FORMAT_CURRENCY =
"#,##0.00"
41NUMBER_FORMAT_EUR =
"#,##0.00 €"
42NUMBER_FORMAT_PERCENT =
"0%"
43NUMBER_FORMAT_DATETIME =
"YYYY-MM-DD HH:MM:SS"
44NUMBER_FORMAT_TIME =
"hh:mm"
46PAGE_MARGIN_FACTOR = 1 / 2.54
51 @brief XLS file creator
52 @param font_name : default font name
53 @param font_size : default font size
56 def __init__(self, font_name: Optional[str] =
None, font_size: Optional[int] =
None):
57 self.
font_name = FONT_NAME
if (font_name
is None)
else font_name
58 self.
font_size = FONT_SIZE
if (font_size
is None)
else font_size
61 def save(self, filename: str) ->
None:
64 @param filename : name of file
68 def set_table(self, worksheet: Worksheet, max_col: int, max_row: int, min_col: int = 1, min_row: int = 1) ->
None:
71 @param worksheet : worksheet
72 @param max_col : maximum column
73 @param max_row : maximum row
74 @param min_col : minimum column (default is first column)
75 @param min_row : worksheet (default is first row)
77 table_style = table.TableStyleInfo(name=
"TableStyleLight15",
79 start_cell = get_column_letter(min_col) + str(min_row)
80 end_cell = get_column_letter(max_col) + str(max_row)
81 new_table = table.Table(ref=f
"{start_cell}:{end_cell}",
82 displayName=worksheet.title,
83 tableStyleInfo=table_style)
84 worksheet.add_table(new_table)
86 def set_page_marcins(self, worksheet: Worksheet, left: Optional[float] =
None, right: Optional[float] =
None,
87 top: Optional[float] =
None, bottom: Optional[float] =
None) ->
None:
89 @brief Set page margins in cm
90 @param worksheet : select worksheet
91 @param left : left margin in cm
92 @param right : left margin in cm
93 @param top : left margin in cm
94 @param bottom : left margin in cm
97 worksheet.page_margins.left = left * PAGE_MARGIN_FACTOR
99 worksheet.page_margins.right = right * PAGE_MARGIN_FACTOR
101 worksheet.page_margins.top = top * PAGE_MARGIN_FACTOR
102 if bottom
is not None:
103 worksheet.page_margins.bottom = bottom * PAGE_MARGIN_FACTOR
107 @brief Set automatic column width of worksheet.
108 @param worksheet : select worksheet
109 @param b_limit : status if width has a max limit
111 for i, col_cells
in enumerate(worksheet.columns, start=1):
113 for j, cell
in enumerate(col_cells):
116 i_max_col_len = max(i_max_col_len, len(str(cell.value).split(
"n", maxsplit=1)[0]))
118 for s_line
in str(cell.value).split(
"/n"):
119 i_max_col_len = max(i_max_col_len, len(s_line))
121 i_max_col_len = min(i_max_col_len, MAX_COLUMN_WIDTH)
122 if i_max_col_len == 0:
123 worksheet.column_dimensions[get_column_letter(i)].hidden =
True
125 worksheet.column_dimensions[get_column_letter(i)].width = (i_max_col_len + 1) * 0.10 * self.
font_size
129 @brief Set automatic row height of worksheet.
130 @param worksheet : select worksheet
131 @param b_limit : status if height has a max limit
133 for i, col_cells
in enumerate(worksheet.rows, start=1):
134 i_high = MAX_ROW_HEIGHT
136 for cell
in col_cells:
137 i_high = max(i_high, len(str(cell.value).split(
"\n")) * MAX_ROW_HEIGHT)
138 worksheet.row_dimensions[i].height = i_high * self.
font_size
140 def set_cell(self, ws: Worksheet, row: int, column: int, value: Optional[str | int | float | datetime | time] =
None,
141 font_name: Optional[str] =
None, color: Optional[str] =
None, font_size: Optional[int] =
None,
142 bold: bool =
False, italic: bool =
False, strike: bool =
False, underline: Optional[str] =
None,
143 vert_align: Optional[str] =
None, fill_color: Optional[str] =
None,
144 align: Optional[str] =
None, align_vert: Optional[str] =
"center", wrap_text: bool =
False,
145 number_format: Optional[str] =
None, border: Optional[Border] =
None) ->
None:
148 Font: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.styles.fonts.html
149 PatternFill: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.styles.fills.html#openpyxl.styles.fills.PatternFill
150 Alignment: https://openpyxl.readthedocs.io/en/latest/api/openpyxl.styles.alignment.html
151 @param ws : worksheet
152 @param row : row position
153 @param column : column position
154 @param value : cell value [numeric, time, string, bool, None]
155 @param font_name : font name (default is Calibri)
156 @param color : font color e.g. "FF0000" for red
157 @param font_size : font size
158 @param bold : status if cell content should be bold
159 @param italic : status if cell content should be italic
160 @param strike : strike option
161 @param underline : underline options ['double', 'doubleAccounting', 'single', 'singleAccounting']
162 @param vert_align : vertical align options ['superscript', 'subscript', 'baseline']
163 @param fill_color : background fill color of cell
164 @param align : horizontal text align option ["general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"]
165 @param align_vert : vertical text align of cell ["top", "center", "bottom", "justify", "distributed"]
166 @param wrap_text : wrap text
167 @param number_format : number format of cell
168 @param border : border of cell
170 if font_name
is None:
172 if font_size
is None:
174 cell = ws.cell(row=row, column=column, value=value)
175 cell.font = Font(name=font_name, color=color, size=str(font_size), bold=bold, italic=italic,
176 strikethrough=strike, underline=underline, vertAlign=vert_align)
177 if fill_color
is not None:
178 cell.fill = PatternFill(fill_type=
"solid", start_color=fill_color, end_color=fill_color)
179 cell.alignment = Alignment(horizontal=align, vertical=align_vert, wrap_text=wrap_text)
180 if number_format
is not None:
181 cell.number_format = number_format
182 if border
is not None:
None set_table(self, Worksheet worksheet, int max_col, int max_row, int min_col=1, int min_row=1)
Set table.
__init__(self, Optional[str] font_name=None, Optional[int] font_size=None)
None set_cell(self, Worksheet ws, int row, int column, Optional[str|int|float|datetime|time] value=None, Optional[str] font_name=None, Optional[str] color=None, Optional[int] font_size=None, bool bold=False, bool italic=False, bool strike=False, Optional[str] underline=None, Optional[str] vert_align=None, Optional[str] fill_color=None, Optional[str] align=None, Optional[str] align_vert="center", bool wrap_text=False, Optional[str] number_format=None, Optional[Border] border=None)
Set cell data Font: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.styles....
None save(self, str filename)
Save file.
None set_column_autowidth(self, Worksheet worksheet, bool b_limit=True)
Set automatic column width of worksheet.
None set_page_marcins(self, Worksheet worksheet, Optional[float] left=None, Optional[float] right=None, Optional[float] top=None, Optional[float] bottom=None)
Set page margins in cm.
None set_row_autoheight(self, Worksheet worksheet, bool b_limit=True)
Set automatic row height of worksheet.