Tuesday 23 May 2017

python - Relative Hyperlinks in QGIS without end user's software dependency


I am trying to work out how can I add a relative path hyperlink to a shapefile in QGIS. I have found several great tips on various forums but only one has so far potential to be implemented. The overall goal is for the end user to be able to access a hyperlinked pdf/png/jpg etc without me having to set up any software dependencies under layer properties Actions tab in QGIS. I assume that the end user will have the software required to open a document but I would like the end user's computer to choose a default program to open the files. One of the possibilities would be to use a 'Python' action which I located on one of the forums which after several modifications did not work for me: http://lists.osgeo.org/pipermail/qgis-user/2011-May/012117.html.


Here is the screenshot of my layer's action properties: enter image description here


In the attribute table, the column with hyperlink is called 'PDF and the relative hyperlink is: /doc/document.pdf


My folder structure is:


 - /HyperlinksTest
-- /data
--- Renewable_Energy_Zones.shp

-- /doc
--- document.pdf
HyperlinksTesting.qgis

When I identify the feature, I get the following Python error:


enter image description here


Am I using wrong variables in the Python code? Is there any other way of using relative paths in hyperlinking data to external documents? I noticed there is eVis extension but as far as I see it relies on specyfying software to open a document with, which I'd like to avoid.


Many thanks, Magda



Answer



Something like this should work:



from os import startfile
from os.path import abspath, dirname, join
proj = QgsProject.instance()
urfile = str(proj.fileName())
path = join(abspath(dirname(urfile)), "HyperlinksTest", "[% PDF %]")
startfile(path)

You can just paste this in as in you don't need to have it all one one line.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...