-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_tables_piMuPDF.py
More file actions
30 lines (22 loc) · 1.21 KB
/
extract_tables_piMuPDF.py
File metadata and controls
30 lines (22 loc) · 1.21 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
import sys, pathlib, pymupdf, pymupdf4llm
from tabulate import tabulate
import pandas
fname = sys.argv[1]
with pymupdf.open(fname) as doc:
for page in doc:
tables=page.find_tables()
for table in tables:
with open("Extracted_tables_pimupdf","a") as table_file:
table_file.write(tabulate(table.extract(),
headers="firstrow", # First row as headers
tablefmt="grid", # Grid format with borders
stralign="left")) # Left align text
#Extract tables as markdown
with open("Extracted_tables_markdown","a") as table_file:
table_file.write(table.to_markdown() + "\n *3")
#Extract tables as html
with open("Extracted_tables_markdown.html","a") as table_file:
table_file.write(table.to_pandas().to_html()+ "\n *3")
#Extract tables as html
with open("Extracted_tables_markdown.json","a") as table_file:
table_file.write(table.to_pandas().to_json()+ "\n *3")