tablepic 0.0.4__tar.gz → 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tablepic
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A package can generate a picture which contains a table.
5
5
  Author-email: Xin Yan <slash.xin@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/slash-xin/tablepic
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "tablepic"
7
- version = "0.0.4"
7
+ version = "0.0.6"
8
8
  authors = [
9
9
  { name="Xin Yan", email="slash.xin@gmail.com" },
10
10
  ]
@@ -2,7 +2,7 @@
2
2
  Author: xinyan
3
3
  Date: 2023-06-14 23:43:57
4
4
  LastEditors: xinyan
5
- LastEditTime: 2023-06-20 14:10:03
5
+ LastEditTime: 2023-07-05 11:36:19
6
6
  Description: file content
7
7
  '''
8
8
  import tablepic as tp
@@ -2,7 +2,7 @@
2
2
  Author: xinyan
3
3
  Date: 2023-06-13 17:12:25
4
4
  LastEditors: xinyan
5
- LastEditTime: 2023-06-20 12:54:03
5
+ LastEditTime: 2023-11-21 12:19:12
6
6
  Description: file content
7
7
  '''
8
8
 
@@ -101,13 +101,13 @@ def generate_table_rec_coord(row_num:int, col_num:int, start_pos:list, margin:in
101
101
  table_pos = [start_pos[0]-margin, start_pos[1]-margin, max_x+margin, max_y+margin]
102
102
  return rec_pos_list, table_pos
103
103
 
104
- def process_title_footnote(title_or_footnote:list, type:str) -> list:
104
+ def process_title_footnote(title_or_footnote:list, default_align:str, default_font_size:int, default_height:int, default_color:str) -> list:
105
105
  result_list = []
106
106
  for idx, content_dict in enumerate(title_or_footnote):
107
- content_dict['font_size'] = content_dict.get('font_size', 40) if idx == 0 and type == 'title' else content_dict.get('font_size', 30)
108
- content_dict['color'] = content_dict.get('color', '#000000')
109
- content_dict['align'] = content_dict.get('align', 'center') if type == 'title' else content_dict.get('align', 'right')
110
- content_dict['height'] = content_dict.get('height', 80) if idx == 0 and type == 'title' else content_dict.get('height', 60)
107
+ content_dict['font_size'] = content_dict.get('font_size', default_font_size) if idx == 0 else content_dict.get('font_size', default_font_size-10)
108
+ content_dict['color'] = content_dict.get('color', default_color)
109
+ content_dict['align'] = content_dict.get('align', default_align)
110
+ content_dict['height'] = content_dict.get('height', default_height) if idx == 0 else content_dict.get('height', default_height-20)
111
111
  result_list.append(content_dict)
112
112
  return result_list
113
113
 
@@ -125,9 +125,10 @@ def get_data_info(data_dict:dict, key:str, i, j, v_default, v_type):
125
125
  return result
126
126
 
127
127
 
128
- def calculate_cell_width_height(data_dict:dict, font_path:str, cell_width:int, cell_height:int):
128
+ def calculate_data_cell_width_height(row_num:int, data_dict:dict, font_path:str, cell_width:int, cell_height:int):
129
129
  col_width_dict = {}
130
130
  row_height_dict = {}
131
+ header_row = row_num - len(data_dict['content'])
131
132
  for i, data_line in enumerate(data_dict['content']):
132
133
  for j, data_content in enumerate(data_line):
133
134
  data_font = get_font(font_path, get_data_info(data_dict, 'font_size', i, j, 20, int))
@@ -135,8 +136,8 @@ def calculate_cell_width_height(data_dict:dict, font_path:str, cell_width:int, c
135
136
  for item in data_content.split('\n'):
136
137
  w, h = data_font.getbbox(item)[2:]
137
138
  c_w = max(c_w, w) + 20
138
- c_h = h * len(data_content.split('\n'))
139
- row_height_dict[i] = max(row_height_dict.get(i, cell_height), c_h)
139
+ c_h = h * len(data_content.split('\n')) + 20
140
+ row_height_dict[i + header_row] = max(row_height_dict.get(i + header_row, cell_height), c_h)
140
141
  col_width_dict[j] = max(col_width_dict.get(j, cell_width), c_w)
141
142
  return col_width_dict, row_height_dict
142
143
 
@@ -198,19 +199,33 @@ def generate_table_pic(row_num:int, col_num:int, title_list:list, header_dict:di
198
199
  :param table_line_color: str, optional parameter. Set the line color of the table, default value is '#E8EAED'.
199
200
  :param font_path: str, optional parameter. Given the font path to set a new font for the text (including title, header, data).
200
201
  """
202
+ # Define default values
203
+ __default_title_font_size = 40
204
+ __default_footnote_font_size = 30
205
+ __default_header_font_size = 30
206
+ __default_data_font_size = 20
207
+ __default_title_height = 80
208
+ __default_footnote_height = 60
209
+ __default_header_bk_color = '#CCD6EB'
210
+ __default_title_color = '#000000'
211
+ __default_footnote_color = '#000000'
212
+ __default_title_align = 'center'
213
+ __default_footnote_align = 'right'
214
+
201
215
  # Calculate the width for each column and the height for each row
202
- if len(row_height_dict) == 0 or len(col_width_dict) == 0:
203
- col_width_dict, row_height_dict = calculate_cell_width_height(data_dict, font_path, cell_width, cell_height)
216
+ calc_col_width_dict, calc_row_height_dict = calculate_data_cell_width_height(row_num, data_dict, font_path, cell_width, cell_height)
217
+ calc_col_width_dict.update(col_width_dict)
218
+ calc_row_height_dict.update(row_height_dict)
204
219
 
205
220
  color_white = '#FFFFFF'
206
221
  color_black = '#000000'
207
- color_default_header_bk = '#CCD6EB'
208
- title_list = process_title_footnote(title_list, 'title')
209
- footnote_list = process_title_footnote(footnote_list, 'footnote')
222
+
223
+ title_list = process_title_footnote(title_list, __default_title_align, __default_title_font_size, __default_title_height, __default_title_color)
224
+ footnote_list = process_title_footnote(footnote_list, __default_footnote_align, __default_footnote_font_size, __default_footnote_height, __default_footnote_color)
210
225
  total_title_height = sum([title['height'] for title in title_list])
211
226
  total_footnote_height = sum([footnote['height'] for footnote in footnote_list])
212
227
  cell_pos_list, table_pos = generate_table_rec_coord(row_num, col_num, start_pos=[table_margin, total_title_height], cell_width=cell_width, cell_height=cell_height,
213
- col_width_dict=col_width_dict, row_height_dict=row_height_dict, cell_merge_dict=cell_merge_dict)
228
+ col_width_dict=calc_col_width_dict, row_height_dict=calc_row_height_dict, cell_merge_dict=cell_merge_dict)
214
229
  pic_width = table_pos[2] + table_margin
215
230
  pic_height = table_pos[3] + table_margin + total_footnote_height
216
231
 
@@ -230,15 +245,15 @@ def generate_table_pic(row_num:int, col_num:int, title_list:list, header_dict:di
230
245
  # Draw Header
231
246
  for idx, header_content in enumerate(header_dict['content']):
232
247
  header_rec_pos = cell_pos_list[idx]
233
- header_font = get_font(font_path, header_dict.get('font_size', 30)) \
234
- if type(header_dict.get('font_size', 30)) == int \
235
- else get_font(font_path, header_dict['font_size'].get(idx, 30))
248
+ header_font = get_font(font_path, header_dict.get('font_size', __default_header_font_size)) \
249
+ if type(header_dict.get('font_size', __default_header_font_size)) == int \
250
+ else get_font(font_path, header_dict['font_size'].get(idx, __default_header_font_size))
236
251
  header_coord = get_content_pos(header_rec_pos, header_content, header_font, header_dict.get('align', 'center')) \
237
252
  if type(header_dict.get('align', 'center')) == str \
238
253
  else get_content_pos(header_rec_pos, header_content, header_font, header_dict['align'].get(idx, 'center'))
239
- draw.rectangle(header_rec_pos, fill=header_dict.get('bk_color', color_default_header_bk)) \
240
- if type(header_dict.get('bk_color', color_default_header_bk)) == str \
241
- else draw.rectangle(header_rec_pos, fill=header_dict['bk_color'].get(idx, color_default_header_bk))
254
+ draw.rectangle(header_rec_pos, fill=header_dict.get('bk_color', __default_header_bk_color)) \
255
+ if type(header_dict.get('bk_color', __default_header_bk_color)) == str \
256
+ else draw.rectangle(header_rec_pos, fill=header_dict['bk_color'].get(idx, __default_header_bk_color))
242
257
  header_fore_color = header_dict.get('fore_color', color_black) \
243
258
  if type(header_dict.get('fore_color', color_black)) == str \
244
259
  else header_dict['fore_color'].get(idx, color_black)
@@ -250,7 +265,7 @@ def generate_table_pic(row_num:int, col_num:int, title_list:list, header_dict:di
250
265
  data_rec_coord = cell_pos_list[idx + 1 + (i * col_num) + j]
251
266
  data_rec_bk_color = get_data_info(data_dict, 'bk_color', i, j, color_white, str)
252
267
  draw.rectangle(data_rec_coord, fill=data_rec_bk_color)
253
- data_font = get_font(font_path, get_data_info(data_dict, 'font_size', i, j, 20, int))
268
+ data_font = get_font(font_path, get_data_info(data_dict, 'font_size', i, j, __default_data_font_size, int))
254
269
  data_coord = get_content_pos(data_rec_coord, data_content, data_font, get_data_info(data_dict, 'align', i, j, 'center', str))
255
270
  draw.text(data_coord, data_content, font=data_font, fill=get_data_info(data_dict, 'fore_color', i, j, color_black, str))
256
271
 
@@ -259,7 +274,7 @@ def generate_table_pic(row_num:int, col_num:int, title_list:list, header_dict:di
259
274
  for idx, footnote in enumerate(footnote_list):
260
275
  footnote_rec_coord = [0, tp_dict[idx-1], pic_width, footnote['height'] + tp_dict[idx-1]]
261
276
  tp_dict[idx] = footnote_rec_coord[3]
262
- footnote_font = get_font(font_path, footnote['font_size'])
277
+ footnote_font = get_font(font_path, footnote.get['font_size'])
263
278
  footnote_coord = get_content_pos(footnote_rec_coord, footnote['content'], footnote_font, footnote['align'])
264
279
  draw.text(footnote_coord, footnote['content'], font=footnote_font, fill=footnote['color'])
265
280
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tablepic
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: A package can generate a picture which contains a table.
5
5
  Author-email: Xin Yan <slash.xin@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/slash-xin/tablepic
File without changes
File without changes
File without changes
File without changes