Identify the table header. *** PyMuPDF extension. *** Starting from the first line above the table upwards, check if it qualifies to be part of the table header. Criteria include: * A one-line table never has an extra header. * Column borders must n
(self, y_tolerance=3)
| 1701 | return pd.DataFrame(pd_dict) |
| 1702 | |
| 1703 | def _get_header(self, y_tolerance=3): |
| 1704 | """Identify the table header. |
| 1705 | |
| 1706 | *** PyMuPDF extension. *** |
| 1707 | |
| 1708 | Starting from the first line above the table upwards, check if it |
| 1709 | qualifies to be part of the table header. |
| 1710 | |
| 1711 | Criteria include: |
| 1712 | * A one-line table never has an extra header. |
| 1713 | * Column borders must not intersect any word. If this happens, all |
| 1714 | text of this line and above of it is ignored. |
| 1715 | * No excess inter-line distance: If a line further up has a distance |
| 1716 | of more than 1.5 times of its font size, it will be ignored and |
| 1717 | all lines above of it. |
| 1718 | * Must have same text properties. |
| 1719 | * Starting with the top table line, a bold text property cannot change |
| 1720 | back to non-bold. |
| 1721 | |
| 1722 | If not all criteria are met (or there is no text above the table), |
| 1723 | the first table row is assumed to be the header. |
| 1724 | """ |
| 1725 | page = self.page |
| 1726 | y_delta = y_tolerance |
| 1727 | |
| 1728 | def top_row_bg_color(self): |
| 1729 | """ |
| 1730 | Compare top row background color with color of same-sized bbox |
| 1731 | above. If different, return True indicating that the original |
| 1732 | table top row is already the header. |
| 1733 | """ |
| 1734 | bbox0 = pymupdf.Rect(self.rows[0].bbox) |
| 1735 | bboxt = bbox0 + (0, -bbox0.height, 0, -bbox0.height) # area above |
| 1736 | top_color0 = page.get_pixmap(clip=bbox0).color_topusage()[1] |
| 1737 | top_colort = page.get_pixmap(clip=bboxt).color_topusage()[1] |
| 1738 | if top_color0 != top_colort: |
| 1739 | return True # top row is header |
| 1740 | return False |
| 1741 | |
| 1742 | def row_has_bold(bbox): |
| 1743 | """Check if a row contains some bold text. |
| 1744 | |
| 1745 | If e.g. true for the top row, then it will be used as (internal) |
| 1746 | column header row if any of the following is true: |
| 1747 | * the previous (above) text line has no bold span |
| 1748 | * the second table row text has no bold span |
| 1749 | |
| 1750 | Returns True if any spans are bold else False. |
| 1751 | """ |
| 1752 | return any( |
| 1753 | c["bold"] |
| 1754 | for c in CHARS |
| 1755 | if rect_in_rect((c["x0"], c["y0"], c["x1"], c["y1"]), bbox) |
| 1756 | ) |
| 1757 | |
| 1758 | try: |
| 1759 | row = self.rows[0] |
| 1760 | cells = row.cells |
no test coverage detected