-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4llm.py
More file actions
177 lines (100 loc) · 4.19 KB
/
Copy path4llm.py
File metadata and controls
177 lines (100 loc) · 4.19 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pymupdf4llm
from pathlib import Path
def doRapOCR(img):
from rapidocr import RapidOCR
engine = RapidOCR()
result, _ = engine(img)
ocr_output = []
for line in result:
box, text, conf = line
# Convert polygon -> bounding box
x_coords = [p[0] for p in box]
y_coords = [p[1] for p in box]
bbox = [min(x_coords), min(y_coords), max(x_coords), max(y_coords)]
ocr_output.append((bbox, text, conf))
return ocr_output
def mdImages():
pass
def main():
print("\n\tPyMuPDF4LLM Command Line App\n")
print("\n\n","Whole file(s) to MD:\t\t|","\tfilemd","\tdirmd","\tnohdft",)
print("\n\n","To MD with Images:\t\t|","\tfilemdi","\tfilemdi64",)
print("\n\n","Force OCR:\t\t|","\tfullocr",)
print("\n\n","Table strategy:\t\t|",)
#print("\n\n","Images:\t\t|",)
#print("\n\n","Forms:\t|",)
#print("\n\n","Transform:\t|",)
#print("\n\n","Export:\t|",)
print("\n\n","Multiple parameters:\t\t|","\tcombine", )
print("\n")
goto = input("\nFunction: ")
print("\n")
if(goto == "filemd"):
print("Single File to Markdown\n")
filename = input("Filename: ")
md = pymupdf4llm.to_markdown(filename,show_progress=True)
#print("Processing...")
if input("Write MD file? (y/n): ").lower() == "y":
output_path = Path(filename).with_suffix(".md")
output_path.write_text(md, encoding="utf-8")
print(f"Wrote markdown to: {output_path}")
else:
print("\n", md, "\n")
elif(goto == "dirmd"):
print("Directory to Markdown\n")
wdir = input("Directory (defaults to CWD): ") or Path.cwd()
wlkpath = Path(wdir)
for file_path in wlkpath.iterdir():
if(file_path.suffix == ".pdf"):
print(file_path.name)
try:
md = pymupdf4llm.to_markdown(file_path,show_progress=True)
except:
print("Error")
print("All files have been processed")
elif(goto == "nohdft"):
print("Single File to Markdown (Header and Footer Removed)\n")
filename = input("Filename: ")
md = pymupdf4llm.to_markdown(filename, header=False, footer=False, show_progress=True)
#print("Processing...")
if input("Write MD file? (y/n): ").lower() == "y":
output_path = Path(filename).with_suffix(".md")
output_path.write_text(md, encoding="utf-8")
print(f"Wrote markdown to: {output_path}")
else:
print("\n", md, "\n")
elif(goto == "filemdi"):
print("Single File to Markdown (Images included in folder)\n")
filename = input("Filename: ")
img_path = Path(filename).parent or Path.cwd()
mdi = pymupdf4llm.to_markdown(filename,show_progress=True,write_images=True,image_path=img_path)
if input("Write MD file? (y/n): ").lower() == "y":
output_path = Path(filename).with_suffix(".md")
output_path.write_text(mdi, encoding="utf-8")
print(f"Wrote markdown to: {output_path}")
else:
print("\n", mdi, "\n")
elif(goto == "filemdi64"):
print("Single File to Markdown (Images included in file as base64)\n")
filename = input("Filename: ")
mdi = pymupdf4llm.to_markdown(filename,show_progress=True,embed_images=True)
output_path = Path(filename).with_suffix(".md")
output_path.write_text(mdi, encoding="utf-8")
print(f"Wrote markdown to: {output_path}")
elif(goto == "fullocr"):
pass
print("Single File to Markdown (Force OCR)\n")
#from rapidocr import RapidOCR
filename = input("Filename: ")
md = pymupdf4llm.to_markdown(filename,show_progress=True,ocr_function=doRapOCR,force_ocr=True) #force_ocr=True, ,ocr_function=doRapOCR
# ocr_function needs to be provided
elif(goto == "quit" or goto == "exit"):
print("Exiting...")
quit()
more = input("Continue? (y/n): ")
if(more.strip().lower() == "y"):
main()
else:
print("Exiting...")
if __name__ == "__main__":
main()