Wednesday, 15 June 2016

Setting character encoding for all layers at once using PyQGIS?


I have to set the character encoding for all layers from system to UTF-8.



Is there a way to change this in all loaded layers at once?


I'm using QGIS 2.8.1 on Windows 7.



Answer



You can use the following which should set the encoding of your loaded layers to UTF-8:


for layer in QgsMapLayerRegistry.instance().mapLayers().values():
layer.setProviderEncoding(u'UTF-8')
layer.dataProvider().setEncoding(u'UTF-8')
print layer.name(), layer.dataProvider().encoding() #Optional check: prints layer name and its encoding source

Remember to save your project file (.qgs) afterwards.





Edit:


As suggested by @meles, you can input the above code into the Python Console (Plugins > Python Console; Extensions > Python Console; Ctrl + Alt + P).


Hope this helps!


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...