table_recover_utils.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. # -*- encoding: utf-8 -*-
  2. # @Author: SWHL
  3. # @Contact: liekkaskono@163.com
  4. import random
  5. from typing import Any, Dict, List, Union, Set, Tuple
  6. import cv2
  7. import numpy as np
  8. import shapely
  9. from shapely.geometry import MultiPoint, Polygon
  10. def sorted_boxes(dt_boxes: np.ndarray) -> np.ndarray:
  11. """
  12. Sort text boxes in order from top to bottom, left to right
  13. args:
  14. dt_boxes(array):detected text boxes with shape (N, 4, 2)
  15. return:
  16. sorted boxes(array) with shape (N, 4, 2)
  17. """
  18. num_boxes = dt_boxes.shape[0]
  19. dt_boxes = sorted(dt_boxes, key=lambda x: (x[0][1], x[0][0]))
  20. _boxes = list(dt_boxes)
  21. # 解决相邻框,后边比前面y轴小,则会被排到前面去的问题
  22. for i in range(num_boxes - 1):
  23. for j in range(i, -1, -1):
  24. if (
  25. abs(_boxes[j + 1][0][1] - _boxes[j][0][1]) < 10
  26. and _boxes[j + 1][0][0] < _boxes[j][0][0]
  27. ):
  28. _boxes[j], _boxes[j + 1] = _boxes[j + 1], _boxes[j]
  29. else:
  30. break
  31. return np.array(_boxes)
  32. def calculate_iou(
  33. box1: Union[np.ndarray, List], box2: Union[np.ndarray, List]
  34. ) -> float:
  35. """
  36. :param box1: Iterable [xmin,ymin,xmax,ymax]
  37. :param box2: Iterable [xmin,ymin,xmax,ymax]
  38. :return: iou: float 0-1
  39. """
  40. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  41. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  42. # 不相交直接退出检测
  43. if b1_x2 < b2_x1 or b1_x1 > b2_x2 or b1_y2 < b2_y1 or b1_y1 > b2_y2:
  44. return 0.0
  45. # 计算交集
  46. inter_x1 = max(b1_x1, b2_x1)
  47. inter_y1 = max(b1_y1, b2_y1)
  48. inter_x2 = min(b1_x2, b2_x2)
  49. inter_y2 = min(b1_y2, b2_y2)
  50. i_area = max(0, inter_x2 - inter_x1) * max(0, inter_y2 - inter_y1)
  51. # 计算并集
  52. b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1)
  53. b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1)
  54. u_area = b1_area + b2_area - i_area
  55. # 避免除零错误,如果区域小到乘积为0,认为是错误识别,直接去掉
  56. if u_area == 0:
  57. return 1
  58. # 检查完全包含
  59. iou = i_area / u_area
  60. return iou
  61. def caculate_single_axis_iou(
  62. box1: Union[np.ndarray, List], box2: Union[np.ndarray, List], axis="x"
  63. ) -> float:
  64. """
  65. :param box1: Iterable [xmin,ymin,xmax,ymax]
  66. :param box2: Iterable [xmin,ymin,xmax,ymax]
  67. :return: iou: float 0-1
  68. """
  69. b1_x1, b1_y1, b1_x2, b1_y2 = box1
  70. b2_x1, b2_y1, b2_x2, b2_y2 = box2
  71. if axis == "x":
  72. i_min = max(b1_x1, b2_x1)
  73. i_max = min(b1_x2, b2_x2)
  74. u_area = max(b1_x2, b2_x2) - min(b1_x1, b2_x1)
  75. else:
  76. i_min = max(b1_y1, b2_y1)
  77. i_max = min(b1_y2, b2_y2)
  78. u_area = max(b1_y2, b2_y2) - min(b1_y1, b2_y1)
  79. i_area = max(i_max - i_min, 0)
  80. if u_area == 0:
  81. return 1
  82. return i_area / u_area
  83. def is_box_contained(
  84. box1: Union[np.ndarray, List], box2: Union[np.ndarray, List], threshold=0.2
  85. ) -> Union[int, None]:
  86. """
  87. :param box1: Iterable [xmin,ymin,xmax,ymax]
  88. :param box2: Iterable [xmin,ymin,xmax,ymax]
  89. :return: 1: box1 is contained 2: box2 is contained None: no contain these
  90. """
  91. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  92. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  93. # 不相交直接退出检测
  94. if b1_x2 < b2_x1 or b1_x1 > b2_x2 or b1_y2 < b2_y1 or b1_y1 > b2_y2:
  95. return None
  96. # 计算box2的总面积
  97. b2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1)
  98. b1_area = (b1_x2 - b1_x1) * (b1_y2 - b1_y1)
  99. # 计算box1和box2的交集
  100. intersect_x1 = max(b1_x1, b2_x1)
  101. intersect_y1 = max(b1_y1, b2_y1)
  102. intersect_x2 = min(b1_x2, b2_x2)
  103. intersect_y2 = min(b1_y2, b2_y2)
  104. # 计算交集的面积
  105. intersect_area = max(0, intersect_x2 - intersect_x1) * max(
  106. 0, intersect_y2 - intersect_y1
  107. )
  108. # 计算外面的面积
  109. b1_outside_area = b1_area - intersect_area
  110. b2_outside_area = b2_area - intersect_area
  111. # 计算外面的面积占box2总面积的比例
  112. ratio_b1 = b1_outside_area / b1_area if b1_area > 0 else 0
  113. ratio_b2 = b2_outside_area / b2_area if b2_area > 0 else 0
  114. if ratio_b1 < threshold:
  115. return 1
  116. if ratio_b2 < threshold:
  117. return 2
  118. # 判断比例是否大于阈值
  119. return None
  120. def is_single_axis_contained(
  121. box1: Union[np.ndarray, List],
  122. box2: Union[np.ndarray, List],
  123. axis="x",
  124. threshold: float = 0.2,
  125. ) -> Union[int, None]:
  126. """
  127. :param box1: Iterable [xmin,ymin,xmax,ymax]
  128. :param box2: Iterable [xmin,ymin,xmax,ymax]
  129. :return: 1: box1 is contained 2: box2 is contained None: no contain these
  130. """
  131. b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
  132. b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
  133. # 计算轴重叠大小
  134. if axis == "x":
  135. b1_area = b1_x2 - b1_x1
  136. b2_area = b2_x2 - b2_x1
  137. i_area = min(b1_x2, b2_x2) - max(b1_x1, b2_x1)
  138. else:
  139. b1_area = b1_y2 - b1_y1
  140. b2_area = b2_y2 - b2_y1
  141. i_area = min(b1_y2, b2_y2) - max(b1_y1, b2_y1)
  142. # 计算外面的面积
  143. b1_outside_area = b1_area - i_area
  144. b2_outside_area = b2_area - i_area
  145. ratio_b1 = b1_outside_area / b1_area if b1_area > 0 else 0
  146. ratio_b2 = b2_outside_area / b2_area if b2_area > 0 else 0
  147. if ratio_b1 < threshold:
  148. return 1
  149. if ratio_b2 < threshold:
  150. return 2
  151. return None
  152. def filter_duplicated_box(table_boxes: List[List[float]]) -> Set[int]:
  153. """
  154. :param table_boxes: [[xmin,ymin,xmax,ymax]]
  155. :return:
  156. """
  157. delete_idx = set()
  158. for i in range(len(table_boxes)):
  159. polygons_i = table_boxes[i]
  160. if i in delete_idx:
  161. continue
  162. for j in range(i + 1, len(table_boxes)):
  163. if j in delete_idx:
  164. continue
  165. # 下一个box
  166. polygons_j = table_boxes[j]
  167. # 重叠关系先记录,后续删除掉
  168. if calculate_iou(polygons_i, polygons_j) > 0.8:
  169. delete_idx.add(j)
  170. continue
  171. # 是否存在包含关系
  172. contained_idx = is_box_contained(polygons_i, polygons_j)
  173. if contained_idx == 2:
  174. delete_idx.add(j)
  175. elif contained_idx == 1:
  176. delete_idx.add(i)
  177. return delete_idx
  178. def sorted_ocr_boxes(
  179. dt_boxes: Union[np.ndarray, list], threshold: float = 0.2
  180. ) -> Tuple[Union[np.ndarray, list], List[int]]:
  181. """
  182. Sort text boxes in order from top to bottom, left to right
  183. args:
  184. dt_boxes(array):detected text boxes with (xmin, ymin, xmax, ymax)
  185. return:
  186. sorted boxes(array) with (xmin, ymin, xmax, ymax)
  187. """
  188. num_boxes = len(dt_boxes)
  189. if num_boxes <= 0:
  190. return dt_boxes, []
  191. indexed_boxes = [(box, idx) for idx, box in enumerate(dt_boxes)]
  192. sorted_boxes_with_idx = sorted(indexed_boxes, key=lambda x: (x[0][1], x[0][0]))
  193. _boxes, indices = zip(*sorted_boxes_with_idx)
  194. indices = list(indices)
  195. _boxes = [dt_boxes[i] for i in indices]
  196. threshold = 20
  197. # 避免输出和输入格式不对应,与函数功能不符合
  198. if isinstance(dt_boxes, np.ndarray):
  199. _boxes = np.array(_boxes)
  200. for i in range(num_boxes - 1):
  201. for j in range(i, -1, -1):
  202. c_idx = is_single_axis_contained(
  203. _boxes[j], _boxes[j + 1], axis="y", threshold=threshold
  204. )
  205. if (
  206. c_idx is not None
  207. and _boxes[j + 1][0] < _boxes[j][0]
  208. and abs(_boxes[j][1] - _boxes[j + 1][1]) < threshold
  209. ):
  210. _boxes[j], _boxes[j + 1] = _boxes[j + 1].copy(), _boxes[j].copy()
  211. indices[j], indices[j + 1] = indices[j + 1], indices[j]
  212. else:
  213. break
  214. return _boxes, indices
  215. def trans_char_ocr_res(ocr_res):
  216. word_result = []
  217. for res in ocr_res:
  218. score = res[2]
  219. for word_box, word in zip(res[3], res[4]):
  220. word_res = []
  221. word_res.append(word_box)
  222. word_res.append(word)
  223. word_res.append(score)
  224. word_result.append(word_res)
  225. return word_result
  226. def box_4_1_poly_to_box_4_2(poly_box: Union[list, np.ndarray]) -> List[List[float]]:
  227. xmin, ymin, xmax, ymax = tuple(poly_box)
  228. return [[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]
  229. def box_4_2_poly_to_box_4_1(poly_box: Union[list, np.ndarray]) -> List[Any]:
  230. """
  231. 将poly_box转换为box_4_1
  232. :param poly_box:
  233. :return:
  234. """
  235. return [poly_box[0][0], poly_box[0][1], poly_box[2][0], poly_box[2][1]]
  236. def match_ocr_cell(dt_rec_boxes: List[List[Union[Any, str]]], pred_bboxes: np.ndarray):
  237. """
  238. :param dt_rec_boxes: [[(4.2), text, score]]
  239. :param pred_bboxes: shap (4,2)
  240. :return:
  241. """
  242. matched = {}
  243. not_match_orc_boxes = []
  244. for i, gt_box in enumerate(dt_rec_boxes):
  245. for j, pred_box in enumerate(pred_bboxes):
  246. pred_box = [pred_box[0][0], pred_box[0][1], pred_box[2][0], pred_box[2][1]]
  247. ocr_boxes = gt_box[0]
  248. # xmin,ymin,xmax,ymax
  249. ocr_box = (
  250. ocr_boxes[0][0],
  251. ocr_boxes[0][1],
  252. ocr_boxes[2][0],
  253. ocr_boxes[2][1],
  254. )
  255. contained = is_box_contained(ocr_box, pred_box, 0.6)
  256. if contained == 1 or calculate_iou(ocr_box, pred_box) > 0.8:
  257. if j not in matched:
  258. matched[j] = [gt_box]
  259. else:
  260. matched[j].append(gt_box)
  261. else:
  262. not_match_orc_boxes.append(gt_box)
  263. return matched, not_match_orc_boxes
  264. def gather_ocr_list_by_row(ocr_list: List[Any], threshold: float = 0.2) -> List[Any]:
  265. """
  266. :param ocr_list: [[[xmin,ymin,xmax,ymax], text]]
  267. :return:
  268. """
  269. threshold = 10
  270. for i in range(len(ocr_list)):
  271. if not ocr_list[i]:
  272. continue
  273. for j in range(i + 1, len(ocr_list)):
  274. if not ocr_list[j]:
  275. continue
  276. cur = ocr_list[i]
  277. next = ocr_list[j]
  278. cur_box = cur[0]
  279. next_box = next[0]
  280. c_idx = is_single_axis_contained(
  281. cur[0], next[0], axis="y", threshold=threshold
  282. )
  283. if c_idx:
  284. dis = max(next_box[0] - cur_box[2], 0)
  285. blank_str = int(dis / threshold) * " "
  286. cur[1] = cur[1] + blank_str + next[1]
  287. xmin = min(cur_box[0], next_box[0])
  288. xmax = max(cur_box[2], next_box[2])
  289. ymin = min(cur_box[1], next_box[1])
  290. ymax = max(cur_box[3], next_box[3])
  291. cur_box[0] = xmin
  292. cur_box[1] = ymin
  293. cur_box[2] = xmax
  294. cur_box[3] = ymax
  295. ocr_list[j] = None
  296. ocr_list = [x for x in ocr_list if x]
  297. return ocr_list
  298. def compute_poly_iou(a: np.ndarray, b: np.ndarray) -> float:
  299. """计算两个多边形的IOU
  300. Args:
  301. poly1 (np.ndarray): (4, 2)
  302. poly2 (np.ndarray): (4, 2)
  303. Returns:
  304. float: iou
  305. """
  306. poly1 = Polygon(a).convex_hull
  307. poly2 = Polygon(b).convex_hull
  308. union_poly = np.concatenate((a, b))
  309. if not poly1.intersects(poly2):
  310. return 0.0
  311. try:
  312. inter_area = poly1.intersection(poly2).area
  313. union_area = MultiPoint(union_poly).convex_hull.area
  314. except shapely.geos.TopologicalError:
  315. print("shapely.geos.TopologicalError occured, iou set to 0")
  316. return 0.0
  317. if union_area == 0:
  318. return 0.0
  319. return float(inter_area) / union_area
  320. def merge_adjacent_polys(polygons: np.ndarray) -> np.ndarray:
  321. """合并相邻iou大于阈值的框"""
  322. combine_iou_thresh = 0.1
  323. pair_polygons = list(zip(polygons, polygons[1:, ...]))
  324. pair_ious = np.array([compute_poly_iou(p1, p2) for p1, p2 in pair_polygons])
  325. idxs = np.argwhere(pair_ious >= combine_iou_thresh)
  326. if idxs.size <= 0:
  327. return polygons
  328. polygons = combine_two_poly(polygons, idxs)
  329. # 注意:递归调用
  330. polygons = merge_adjacent_polys(polygons)
  331. return polygons
  332. def combine_two_poly(polygons: np.ndarray, idxs: np.ndarray) -> np.ndarray:
  333. del_idxs, insert_boxes = [], []
  334. idxs = idxs.squeeze(-1)
  335. for idx in idxs:
  336. # idx 和 idx + 1 是重合度过高的
  337. # 合并,取两者各个点的最大值
  338. new_poly = []
  339. pre_poly, pos_poly = polygons[idx], polygons[idx + 1]
  340. # 四个点,每个点逐一比较
  341. new_poly.append(np.minimum(pre_poly[0], pos_poly[0]))
  342. x_2 = min(pre_poly[1][0], pos_poly[1][0])
  343. y_2 = max(pre_poly[1][1], pos_poly[1][1])
  344. new_poly.append([x_2, y_2])
  345. # 第3个点
  346. new_poly.append(np.maximum(pre_poly[2], pos_poly[2]))
  347. # 第4个点
  348. x_4 = max(pre_poly[3][0], pos_poly[3][0])
  349. y_4 = min(pre_poly[3][1], pos_poly[3][1])
  350. new_poly.append([x_4, y_4])
  351. new_poly = np.array(new_poly)
  352. # 删除已经合并的两个框,插入新的框
  353. del_idxs.extend([idx, idx + 1])
  354. insert_boxes.append(new_poly)
  355. # 整合合并后的框
  356. polygons = np.delete(polygons, del_idxs, axis=0)
  357. insert_boxes = np.array(insert_boxes)
  358. polygons = np.append(polygons, insert_boxes, axis=0)
  359. polygons = sorted_boxes(polygons)
  360. return polygons
  361. def get_rotate_crop_image(img: np.ndarray, points: np.ndarray) -> np.ndarray:
  362. img_crop_width = int(
  363. max(
  364. np.linalg.norm(points[0] - points[1]),
  365. np.linalg.norm(points[2] - points[3]),
  366. )
  367. )
  368. img_crop_height = int(
  369. max(
  370. np.linalg.norm(points[0] - points[3]),
  371. np.linalg.norm(points[1] - points[2]),
  372. )
  373. )
  374. pts_std = np.float32(
  375. [
  376. [0, 0],
  377. [img_crop_width, 0],
  378. [img_crop_width, img_crop_height],
  379. [0, img_crop_height],
  380. ]
  381. )
  382. M = cv2.getPerspectiveTransform(
  383. points.astype(np.float32), pts_std.astype(np.float32)
  384. )
  385. dst_img = cv2.warpPerspective(
  386. img,
  387. M,
  388. (img_crop_width, img_crop_height),
  389. borderMode=cv2.BORDER_REPLICATE,
  390. flags=cv2.INTER_CUBIC,
  391. )
  392. dst_img_height, dst_img_width = dst_img.shape[0:2]
  393. if dst_img_height * 1.0 / dst_img_width >= 1.5:
  394. dst_img = np.rot90(dst_img)
  395. return dst_img
  396. def is_inclusive_each_other(box1: np.ndarray, box2: np.ndarray):
  397. """判断两个多边形框是否存在包含关系
  398. Args:
  399. box1 (np.ndarray): (4, 2)
  400. box2 (np.ndarray): (4, 2)
  401. Returns:
  402. bool: 是否存在包含关系
  403. """
  404. poly1 = Polygon(box1)
  405. poly2 = Polygon(box2)
  406. poly1_area = poly1.convex_hull.area
  407. poly2_area = poly2.convex_hull.area
  408. if poly1_area > poly2_area:
  409. box_max = box1
  410. box_min = box2
  411. else:
  412. box_max = box2
  413. box_min = box1
  414. x0, y0 = np.min(box_min[:, 0]), np.min(box_min[:, 1])
  415. x1, y1 = np.max(box_min[:, 0]), np.max(box_min[:, 1])
  416. edge_x0, edge_y0 = np.min(box_max[:, 0]), np.min(box_max[:, 1])
  417. edge_x1, edge_y1 = np.max(box_max[:, 0]), np.max(box_max[:, 1])
  418. if x0 >= edge_x0 and y0 >= edge_y0 and x1 <= edge_x1 and y1 <= edge_y1:
  419. return True
  420. return False
  421. def plot_html_table(
  422. logi_points: Union[Union[np.ndarray, List]], cell_box_map: Dict[int, List[str]]
  423. ) -> str:
  424. # 初始化最大行数和列数
  425. max_row = 0
  426. max_col = 0
  427. # 计算最大行数和列数
  428. for point in logi_points:
  429. max_row = max(max_row, point[1] + 1) # 加1是因为结束下标是包含在内的
  430. max_col = max(max_col, point[3] + 1) # 加1是因为结束下标是包含在内的
  431. # 创建一个二维数组来存储 sorted_logi_points 中的元素
  432. grid = [[None] * max_col for _ in range(max_row)]
  433. valid_start_row = (1 << 16) - 1
  434. valid_start_col = (1 << 16) - 1
  435. valid_end_col = 0
  436. # 将 sorted_logi_points 中的元素填充到 grid 中
  437. for i, logic_point in enumerate(logi_points):
  438. row_start, row_end, col_start, col_end = (
  439. logic_point[0],
  440. logic_point[1],
  441. logic_point[2],
  442. logic_point[3],
  443. )
  444. ocr_rec_text_list = cell_box_map.get(i)
  445. if ocr_rec_text_list and "".join(ocr_rec_text_list):
  446. valid_start_row = min(row_start, valid_start_row)
  447. valid_start_col = min(col_start, valid_start_col)
  448. valid_end_col = max(col_end, valid_end_col)
  449. for row in range(row_start, row_end + 1):
  450. for col in range(col_start, col_end + 1):
  451. grid[row][col] = (i, row_start, row_end, col_start, col_end)
  452. # 创建表格
  453. table_html = "<html><body><table>"
  454. # 遍历每行
  455. for row in range(max_row):
  456. if row < valid_start_row:
  457. continue
  458. temp = "<tr>"
  459. # 遍历每一列
  460. for col in range(max_col):
  461. if col < valid_start_col or col > valid_end_col:
  462. continue
  463. if not grid[row][col]:
  464. temp += "<td></td>"
  465. else:
  466. i, row_start, row_end, col_start, col_end = grid[row][col]
  467. if not cell_box_map.get(i):
  468. continue
  469. if row == row_start and col == col_start:
  470. ocr_rec_text = cell_box_map.get(i)
  471. text = "<br>".join(ocr_rec_text)
  472. # 如果是起始单元格
  473. row_span = row_end - row_start + 1
  474. col_span = col_end - col_start + 1
  475. cell_content = (
  476. f"<td rowspan={row_span} colspan={col_span}>{text}</td>"
  477. )
  478. temp += cell_content
  479. table_html = table_html + temp + "</tr>"
  480. table_html += "</table></body></html>"
  481. return table_html
  482. def vis_table(img: np.ndarray, polygons: np.ndarray) -> np.ndarray:
  483. for i, poly in enumerate(polygons):
  484. poly = np.round(poly).astype(np.int32).reshape(4, 2)
  485. random_color = (
  486. random.randint(0, 255),
  487. random.randint(0, 255),
  488. random.randint(0, 255),
  489. )
  490. cv2.polylines(img, [poly], 3, random_color)
  491. font = cv2.FONT_HERSHEY_SIMPLEX
  492. cv2.putText(img, str(i), poly[0], font, 1, (0, 0, 255), 1)
  493. return img
  494. def format_html(html):
  495. return f"""
  496. <!DOCTYPE html>
  497. <html lang="zh-CN">
  498. <head>
  499. <meta charset="UTF-8">
  500. <title>Complex Table Example</title>
  501. <style>
  502. table {{
  503. border-collapse: collapse;
  504. width: 100%;
  505. }}
  506. th, td {{
  507. border: 1px solid black;
  508. padding: 8px;
  509. text-align: center;
  510. }}
  511. th {{
  512. background-color: #f2f2f2;
  513. }}
  514. </style>
  515. </head>
  516. <body>
  517. {html}
  518. </body>
  519. </html>
  520. """