-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdtables.py
More file actions
55 lines (30 loc) · 1.14 KB
/
Copy pathmdtables.py
File metadata and controls
55 lines (30 loc) · 1.14 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
55
import pymupdf
def mdsave(res, output):
import pathlib
pathlib.Path(output).write_bytes(res.encode())
filename = input("Filename: ")
doc = pymupdf.open(filename)
hzlst = input("Horizontal strategies (lines/lines_strict/text, default lines): ").strip().lower()
verst = input("Vertical strategies (lines/lines_strict/text, default lines): ").strip().lower()
if(not hzlst):
hzlst = "lines"
if(not verst):
verst = "lines"
for page in doc:
ft = page.find_tables(horizontal_strategy=hzlst, vertical_strategy=verst)
if(len(ft.tables)>0):
for index,table in enumerate(ft.tables):
print("Page #", page.number, "Table #", index)
if(table.header.external):
print(table.header.names)
for row in table.extract():
print(row)
tomd = input("Convert to markdown (y/n): ").strip().lower() == 'y'
if(tomd):
res = table.to_markdown()
output = input("Output filename: ")
if(output.strip()):
mdsave(res, output)
else:
print(res)
doc.close()