3

When Exporting Layer attributes to Excel, file columns are arranged as per their back-end database.

I want to export layer attributes to excel and I want the columns to be arranged in specific order for example if in QGIS the order of column is "column_a", "column_b", "column_c", "column_d" in Excel I want it to be "column_a", "column_c", "column_b", "column_d" and may be skip some columns.

I've read many solutions but they all are just one time solution, in my work i need to export multiple layer attributes daily i want to write a code for that, but i have no python experience.

Can anyone guide me with this task?

5
  • You could try running refactor fields in the graphical modeler, this will allow you to automate the process without any python and refactor allows you to re-organize your columns
    – Sethinacan
    Commented Jan 3, 2023 at 11:21
  • I've tried it, but it does not help.
    – user215296
    Commented Jan 3, 2023 at 11:29
  • I have created a model that refactors the fields and it works fine for me, if you have different columns every time the approach won't be much help. What issue are you having?
    – Sethinacan
    Commented Jan 3, 2023 at 11:32
  • as per my knowledge using refractor needs to upload template every time i need to export file, also it create a shapefile which then will be exported to excel.
    – user215296
    Commented Jan 3, 2023 at 11:59
  • If the attributes are different every time then yes this approach won't help much but if they follow the same template you can save your refactor settings in a model.
    – Sethinacan
    Commented Jan 3, 2023 at 13:19

2 Answers 2

7

I have created a rough model enter image description here

You will need to load in your template attributes and re-organise as desired

enter image description here

1

This will automatically refactor fields accourding to the current order of the attribute table. Reorder the fields as you like, adjust the layer name and:

layer = QgsProject.instance().mapLayersByName("points")[0] #Change points to the name of your layer

#Create a dictionary of current field order and the field properties:
default_fields = {f.name():{'expression':f.name(),'length':f.length(),'name':f.name(),'precision':f.precision(),'sub_type':f.subType(),'type':f.type(),'type_name':f.typeName()} for f in layer.fields()}

#For example to get the properties of the KKOD field:
#default_fields['KKOD']
#{'expression': 'KKOD', 'length': 5, 'name': 'KKOD', 'precision': 0, 'sub_type': 0, 'type': 2, 'type_name': 'Integer'}

#Find the current field order accourding to the attribute table configuration:
tableconfig  = layer.attributeTableConfig()
new_field_order = [c.name for c in tableconfig.columns()] #The current order in the attribute table

#Remap the fields 
newfieldmap = [default_fields[newfield] for newfield in new_field_order if newfield in default_fields]

processing.runAndLoadResults("native:refactorfields", {'INPUT':layer,
    'FIELDS_MAPPING':newfieldmap,'OUTPUT':'TEMPORARY_OUTPUT'})

enter image description here

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.