I have about 40 layers with attribute tables that have many NULL columns. I downloaded the plugin "Delete NULL Fields from Vector Data", which works. I'd like to automate it using the QGIS Python API. The script I tried returned the following:
Traceback (most recent call last): File "C:\PROGRA~1\QGIS32~2.3\apps\Python39\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 17, in File "C:\PROGRA~1/QGIS32~2.3/apps/qgis/./python/plugins\processing\tools\general.py", line 108, in run return Processing.runAlgorithm(algOrName, parameters, onFinish, feedback, context) File "C:\PROGRA~1/QGIS32~2.3/apps/qgis/./python/plugins\processing\core\Processing.py", line 169, in runAlgorithm raise QgsProcessingException(msg) _core.QgsProcessingException: Error: Algorithm qgis:Delete_Null_Fields not found
This is my script:
# specify output folder and new filename
output_folder = '/path/to/output/folder'
new_name = layer.name() + '_nonull'
# get a list of all layers in the project
layers = QgsProject.instance().mapLayers().values()
# loop through each layer and delete null fields
for layer in layers:
# run the plugin
alg_params = {
'INPUT': layer,
'FIELDS': None,
'OUTPUT': output_folder + '/' + new_name + '.shp'
}
nonull_layer = processing.run('qgis:Delete_Null_Fields', alg_params)['OUTPUT']
# add the new layer to the project
QgsProject.instance().addMapLayer(nonull_layer, False)
# save the new layer to a file
file_info = QFileInfo(layer.source())
output_path = file_info.path() + '/' + new_name + '.shp'
QgsVectorLayer.exportLayer(nonull_layer, output_path, 'ESRI Shapefile')
From what I understood, the API was unable to find the plugin from the name I gave. I got it from the dev's git page. I also tried initialising it first using the dev's script before my code starts:
def classFactory(iface): # pylint: disable=invalid-name
"""Load Delete_Null_Fields class from file Delete_Null_Fields.
:param iface: A QGIS interface instance.
:type iface: QgsInterface
"""
#
from .delete_null_fields import Delete_Null_Fields
return Delete_Null_Fields(iface)
But it didn't work. Importantly, I am very new to programming. What is wrong with the code and how could I achieve my batch processing?