Kushalguptaiitb commited on
Commit
bfeed03
·
verified ·
1 Parent(s): 5449355

Delete create_fitz_json.py

Browse files
Files changed (1) hide show
  1. create_fitz_json.py +0 -80
create_fitz_json.py DELETED
@@ -1,80 +0,0 @@
1
- import fitz # PyMuPDF
2
- import json
3
-
4
- def extract_text_to_json_with_bbox(pdf_path):
5
- # Open the PDF
6
- doc = fitz.open(pdf_path)
7
- pdf_name = pdf_path.split("/")[-1] # Extract the PDF filename
8
- result = {}
9
-
10
- # Iterate through each page
11
- for page_num in range(doc.page_count):
12
- page = doc.load_page(page_num)
13
- page_width = page.rect.width
14
- page_height = page.rect.height
15
-
16
- # Get text blocks
17
- blocks = page.get_text("dict")["blocks"]
18
- text_block_id = 0
19
- element_id = 0
20
- page_data = []
21
-
22
- for block in blocks:
23
- if block["type"] == 0: # Text block
24
- for line in block["lines"]:
25
- for span in line["spans"]:
26
- text = span["text"].strip()
27
- if text: # Only process non-empty text
28
- bbox = span["bbox"] # [x0, y0, x1, y1]
29
- font_size = span["size"]
30
- font_type = span["font"]
31
-
32
- # Create the JSON entry
33
- entry = {
34
- "text_block_id": text_block_id,
35
- "element_id": element_id,
36
- "text": text,
37
- "font_size": font_size,
38
- "font_type": font_type,
39
- "pdf_name": pdf_name,
40
- "bbox": list(bbox),
41
- "page_number": page_num + 1,
42
- "page_width": page_width,
43
- "page_height": page_height
44
- }
45
- page_data.append(entry)
46
- element_id += 1
47
- text_block_id += 1
48
-
49
- # Store page data in output dictionary
50
- result[str(page_num + 1)] = page_data
51
-
52
- # Close the PDF
53
- doc.close()
54
- return result
55
-
56
- def save_to_json(data, output_file):
57
- with open(output_file, 'w', encoding='utf-8') as f:
58
- json.dump(data, f, ensure_ascii=False, indent=4)
59
-
60
-
61
-
62
-
63
- if __name__ == "__main__":
64
-
65
- # Example usage
66
- # pdf_path = "/shared_disk/kushal/db_str_chunking/new_ws_structured_code/MILN/data_mike/Home Re 2021-1 Final Offering Circular.pdf"
67
- pdf_path = "/shared_disk/kushal/db_str_chunking/new_ws_structured_code/MILN/data_fitz_test/3_column_layout_sample.pdf"
68
- json_output = extract_text_to_json_with_bbox(pdf_path)
69
-
70
- # Save to JSON file
71
- with open("/shared_disk/kushal/db_str_chunking/new_ws_structured_code/MILN/output/3_column_output.json", "w") as f:
72
- json.dump(json_output, f, indent=4)
73
-
74
- # Print the JSON output
75
- # print(json.dumps(json_output, indent=4))
76
-
77
-
78
-
79
-
80
-