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.

1 Answer 1


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

Syntax for PyQGIS 2:

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    print layer.name(), layer.dataProvider().encoding()   #Optional check: prints layer name and its encoding source

Syntax for PyQGIS 3:

for layer in QgsProject.instance().mapLayers().values():
    print (layer.name(), layer.dataProvider().encoding())

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


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!

  • 1
    That did the trick. Maybe you could mention in your answer that you have to insert the commands in Extension->Python console (Ctrl + Alt + P). That might help other people comming by. Thank you for your help.
    – meles
    Jun 29, 2015 at 10:00
  • @meles - Awesome buddy and your most welcome, glad you got it working! I've edited the post thanks to your suggestion =)
    – Joseph
    Jun 29, 2015 at 10:06
  • @Joseph thanks for sharing what is the difference b.w setProviderEncoding and dataProvider().setEncoding(u'UTF-8') Nov 25, 2016 at 12:17
  • 2
    @MohammadFaizanKhan - Good question! From the API, setProviderEncoding() sets the text encoding of the data provider; dataProvider().setEncoding() sets the encoding used for accessing data from layer. As this was more than a year ago, my guess is they were both required for the data to be encoded and for QGIS to read the encoded data but I can't remember!
    – Joseph
    Nov 25, 2016 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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