pdftext 0.3.4__tar.gz → 0.3.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: pdftext
3
- Version: 0.3.4
3
+ Version: 0.3.6
4
4
  Summary: Extract structured text from pdfs quickly
5
5
  Home-page: https://github.com/VikParuchuri/pdftext
6
6
  License: Apache-2.0
@@ -9,15 +9,13 @@ from pdftext.pdf.utils import get_fontname, pdfium_page_bbox_to_device_bbox, pag
9
9
  from pdftext.settings import settings
10
10
 
11
11
 
12
- def update_previous_fonts(text_chars: Dict, i: int, fontname: str, fontflags: int, prev_fontname: str, text_page, fontname_sample_freq: int):
13
- min_update = max(0, i - fontname_sample_freq + 1) # Minimum index to update
14
- regather_font_info = fontname != prev_fontname
15
- for j in range(min_update, i): # Goes from min_update to i - 1
16
- if regather_font_info:
17
- fontname, fontflags = get_fontname(text_page, j)
12
+ def update_previous_fonts(text_chars: Dict, i: int, prev_fontname: str, prev_fontflags: int, text_page, fontname_sample_freq: int):
13
+ min_update = max(0, i - fontname_sample_freq) # Minimum index to update
14
+ for j in range(i-1, min_update, -1): # Goes from i to min_update
15
+ fontname, fontflags = get_fontname(text_page, j)
18
16
 
19
17
  # If we hit the region with the previous fontname, we can bail out
20
- if fontname == prev_fontname:
18
+ if fontname == prev_fontname and fontflags == prev_fontflags:
21
19
  break
22
20
  text_chars["chars"][j]["font"]["name"] = fontname
23
21
  text_chars["chars"][j]["font"]["flags"] = fontflags
@@ -70,12 +68,14 @@ def get_pdfium_chars(pdf, fontname_sample_freq=settings.FONTNAME_SAMPLE_FREQ, pa
70
68
  fontweight = round(pdfium_c.FPDFText_GetFontWeight(text_page, i), 1)
71
69
  if fontname is None or i % fontname_sample_freq == 0:
72
70
  prev_fontname = fontname
71
+ prev_fontflags = fontflags
73
72
  fontname, fontflags = get_fontname(text_page, i)
74
- update_previous_fonts(text_chars, i, fontname, fontflags, prev_fontname, text_page, fontname_sample_freq)
73
+ if (fontname != prev_fontname or fontflags != prev_fontflags) and i > 0:
74
+ update_previous_fonts(text_chars, i, prev_fontname, prev_fontflags, text_page, fontname_sample_freq)
75
75
 
76
76
  rotation = pdfium_c.FPDFText_GetCharAngle(text_page, i)
77
77
  rotation = rotation * 180 / math.pi # convert from radians to degrees
78
- coords = text_page.get_charbox(i, loose=True)
78
+ coords = text_page.get_charbox(i, loose=rotation == 0) # Loose doesn't work properly when page is rotated
79
79
  device_coords = page_bbox_to_device_bbox(page, coords, page_width, page_height, bl_origin, page_rotation, normalize=True)
80
80
 
81
81
  char_info = {
@@ -8,7 +8,7 @@ class Settings(BaseSettings):
8
8
  MODEL_PATH: str = os.path.join(BASE_PATH, "models", "dt.joblib")
9
9
 
10
10
  # Fonts
11
- FONTNAME_SAMPLE_FREQ: int = 10
11
+ FONTNAME_SAMPLE_FREQ: int = 4
12
12
 
13
13
  # Inference
14
14
  BLOCK_THRESHOLD: float = 0.8 # Confidence threshold for block detection
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "pdftext"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = "Extract structured text from pdfs quickly"
5
5
  authors = ["Vik Paruchuri <vik.paruchuri@gmail.com>"]
6
6
  license = "Apache-2.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes