-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirlinks.py
More file actions
65 lines (33 loc) · 1.19 KB
/
Copy pathdirlinks.py
File metadata and controls
65 lines (33 loc) · 1.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
import pathlib
import pymupdf
print("Get links in a directory")
wdir = input("Directory (defaults to CWD): ")
if(wdir == ""):
wdir = pathlib.Path.cwd()
wlkpath = pathlib.Path(wdir)
all_links = []
for file_path in wlkpath.iterdir():
if(file_path.suffix == ".pdf"):
print(file_path.name)
try:
doc = pymupdf.open(file_path)
for page in doc:
links = page.get_links()
if(len(links)>0):
print("Page",page.number,"\n")
for lnk in links:
if(lnk["kind"] == 2):
print("\t",lnk["uri"],"\n")
all_links.append(lnk["uri"])
doc.close()
except:
print("Error")
if all_links:
txtfile = input("Output filename (defaults to pdf_links.txt): ") or "pdf_links.txt"
output_file = wdir / pathlib.Path(txtfile)
output_file.write_text("\n".join(all_links), encoding="utf-8")
print(f"\nSaved links to {output_file}")
else:
print("\nNo links found.")
"""filename = input("Filename: ")
doc = pymupdf.open(filename)"""