I'm generating maps with QGis. What I do is generate a bunch of non-spatial data on a set of cities (using VBA), and still using VBA, generate a python code that will automaticaly output general map on the studied territory.

So far, I was importing the data into Qgis with the following code :

lyrData = QgsVectorLayer(u'C:/SomePlace/SomeFile.xlsx', ' ', 'ogr')

Which seemed to work, until it started to treat the header of the attributes/columns as data for some territories. Note that this bug is consistent : for some territories it will import header as header and for some it will import header as data...

I tried different things so far : If I import manually (without pyQgis), the bug is still here. I tried renaming the .xlsx file with the name of one that works, but it didn't work.

Another thing I tried is to import the file as an .csv instead of .xslx, but Qgis Cookbook only give exemple to import spatial data csv :

uri = "/some/path/file.csv?delimiter=%s&xField=%s&yField=%s" % (";", "x", "y")
vlayer = QgsVectorLayer(uri, "layer_name_you_like", "delimitedtext")


uri = "file:///some/path/file.csv?delimiter=%s&crs=epsg:4723&wktField=%s" % (";", "shape")

And this, despite the fact that you can manually import CSV files without geometry

Using PyQGIS, is there a way to precise that imported excel files have an header, or is there a way to import csv files without geometry?


So I've found out what was it !

I added into the code a column with city names to add as labels on the map. But it only contain a small number of them, and Qgis probably don't like Null values that much...

After replacing them with zeros the importation of the .xlsx files went smoothly ;) !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.