-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader4.py
More file actions
54 lines (42 loc) · 1.99 KB
/
reader4.py
File metadata and controls
54 lines (42 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import fitz # PyMuPDF
def extract_transactions_table_only(input_pdf_path, output_pdf_path):
doc = fitz.open(input_pdf_path)
output_doc = fitz.open()
for page_num in range(len(doc)):
page = doc.load_page(page_num)
blocks = page.get_text("blocks") # Get text blocks with positions
# Define header region (top 15% of page height)
header_threshold = page.rect.height * 0.15
found_header = False
for block in blocks:
x0, y0, x1, y1, text, *_ = block
if text.strip() == "TRANSACTIONS" and y1 < header_threshold:
found_header = True
break
if found_header:
# Define table region (below header line)
table_start_y = header_threshold + 10 # Start just below header
table_blocks = [
block for block in blocks
if block[1] > table_start_y and block[4].strip() != ""
]
if table_blocks:
# Sort blocks top to bottom
table_blocks.sort(key=lambda b: b[1])
# Combine text lines
table_text = "\n".join(block[4].strip() for block in table_blocks)
# Create new page and write table
new_page = output_doc.new_page(width=595, height=842) # A4
new_page.insert_text((50, 50), f"Page {page_num + 1} - TRANSACTIONS Table", fontsize=12)
new_page.insert_textbox(fitz.Rect(50, 80, 545, 800), table_text, fontsize=10, fontname="courier")
if output_doc.page_count > 0:
output_doc.save(output_pdf_path)
print(f"✅ Extracted tables saved to: {output_pdf_path}")
else:
print("⚠️ No pages with exact 'TRANSACTIONS' header found.")
doc.close()
output_doc.close()
# 🔧 Replace with your actual paths
input_pdf = r"C:\Users\Suren\Downloads\input.pdf"
output_pdf = r"C:\Users\Suren\Downloads\transactions_output.pdf"
extract_transactions_table_only(input_pdf, output_pdf)