-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPDFtoMD.py
66 lines (61 loc) Β· 1.97 KB
/
PDFtoMD.py
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
#!/usr/bin/env python
import time
import requests
import json
from api_keys import MathPic_KEY
APP_KEY = MathPic_KEY
def convert(file):
# REQUEST PDF ID
# =============================================================================
# send PDF via local file
options = {
"conversion_formats": {"md": True},
"math_inline_delimiters": ["$", "$"],
"rm_spaces": True,
"enable_spell_check": False
}
r = requests.post("https://api.mathpix.com/v3/pdf",
headers={
"app_id": "APP_ID",
"app_key": APP_KEY
},
data={
"options_json": json.dumps(options)
},
files={
"file": open(file,"rb")
}
)
PDF_ID = r.json()["pdf_id"]
print(PDF_ID)
# =============================================================================
# REQUEST STATUS
processing = True
while processing:
# every 5 seconds, check the status of the PDF
# if the status is "completed", then request the conversion to MD
time.sleep(10) # pause for 5 seconds
# Perform a GET request
r = requests.get("https://api.mathpix.com/v3/pdf/" + str(PDF_ID),
headers={
"app_id": PDF_ID,
"app_key": APP_KEY,
"Content-type": "application/json"
}
)
STATUS = r.json()
print(STATUS)
if STATUS["status"] == "completed":
processing = False
# REQUEST PDF CONVERSION TO MD
# =============================================================================
headers = {
"app_key": APP_KEY,
"app_id": PDF_ID
}
url = "https://api.mathpix.com/v3/pdf/" + PDF_ID + ".md"
response = requests.get(url, headers=headers)
print(response.text)
with open("PDFasMD.md", "w", encoding='utf-8') as f:
f.write(response.text)
print("PDFasMD.md created!!!")