page.insert_link() in fitz. LINK_LAUNCH escape the ":" in the file path #3445
-
import fitz
doc = fitz.open("input.pdf")
page = doc[1]
rl = page.search_for("cat")
file_path = r'C:/Users/LENOVO/PycharmProjects/pythonProject/2.pdf'
for rect in rl:
page.insert_link({"kind": fitz.LINK_LAUNCH, "from": rect, "page": 0,"to": fitz.Point(0,0),'file':file_path})
doc.save("output.pdf")
doc.close() The hyperlink won't open after testing. import fitz
doc = fitz.open("output.pdf")
page = doc[1]
lnks = page.get_links()
for l in lnks:
print(l)
doc.close() Here are the results:
Apparently the ":" was escaped as "%3A" 3.Replace the code:
4.succeeds
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You must use a valid HTML file specification to make this work, like |
Beta Was this translation helpful? Give feedback.
-
This script works and adds 2 working links (in relevant viewers on Windows): import fitz
doc = fitz.open()
r1 = fitz.Rect(0, 0, 50, 50)
r2 = fitz.Rect(0, 50, 50, 100)
l1 = {
"kind": fitz.LINK_LAUNCH,
"from": r1,
"file": "C:/Users/xxxxx/OneDrive/Desktop/roque.xlsx",
}
l2 = {
"kind": fitz.LINK_URI,
"from": r2,
"uri": "file://C:/Users/xxxxx/OneDrive/Desktop/roque.xlsx",
}
page = doc.new_page()
page.insert_link(l1)
page.insert_link(l2)
doc.save("x.pdf") A "page" specification makes no sense in either case! Use LINK_GOTOR if you need this. The respective generated PDF definitions are 4 0 obj
<</Type/Page/MediaBox[0 0 595 842]/Rotate 0/Resources 3 0 R/Parent 2 0 R/Annots[5 0 R 6 0 R]>>
endobj
5 0 obj
<</A<</S/Launch/F<</Type/Filespec/F(C:/Users/xxxxx/OneDrive/Desktop/roque.xlsx)/UF(C:/Users/xxxxx/OneDrive/Desktop/roque.xlsx)>>>>/Rect[0 792 50 842]/BS<</W 0>>/Subtype/Link/NM(fitz-L0)>>
endobj
6 0 obj
<</A<</S/URI/URI(file://C:/Users/xxxxx/OneDrive/Desktop/roque.xlsx)>>/Rect[0 742 50 792]/BS<</W 0>>/Subtype/Link/NM(fitz-L1)>>
endobj |
Beta Was this translation helpful? Give feedback.
This script works and adds 2 working links (in relevant viewers on Windows):
A "page" specification makes no sense in either case! Use LINK_GOTOR if you need this.
The respective generated PDF definitions are
4 0 obj <</Type/Page/MediaBox[0 0 595 842]/Rotate 0/Resources 3 0 R/Parent 2 0 R/Annot…