I've written around 30 scripts (both QGIS and non-QGIS scripts) in Python 2.7 and using it through QGIS 2.18 "Processing -> Scripts" tools so that within our office we can share and use the scripts.
We have moved to QGIS 3.4 and no issues with converting almost all of the 30 scripts from Python 2.7 to Python 3.7 and as of now, I am able to run those scripts through "QGIS Python Console". But i don't know how to convert those scripts to "Python processing tool scripts" which can be loaded in QGIS 3.4 and execute the scripts as I used to do in QGIS 2.18.
As an example, would like to brief about a script i am using - "Delete_lines_from_CSV"
1) Input will be a CSV file with 1st row - will have the list of CSVs for which the lines are to be deleted; IInd row - will have the corresponding new csv file name.
Below is the code I am using and it runs in the QGIS 3.4 Python Console without any issues.
import os, csv, re
from os import path, linesep
input_csvpath = r"D:\Test\pyqgis3\csvlist2.csv"
nlines = 2
nfirstlines = []
with open(input_csvpath, "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
input_csv= row[0]
out_csv = row[1]
with open(input_csv) as incsv, open(out_csv, "w") as outcsv:
for x in range(int(nlines)):
nfirstlines.append(next(incsv))
for line in incsv:
outcsv.write(line)
With the help of the links and comments mentioned in this thread, Writing a python processing script with Qgis 3.0 - I've modified the template code to the very basics
from PyQt5.QtCore import (QCoreApplication,
QVariant)
from qgis.core import (QgsProcessing,
QgsFields,
QgsField,
QgsProcessingException,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingParameterFile,
QgsProcessingParameterString,
QgsProcessingParameterBoolean,
QgsProcessingParameterNumber,
)
import os, csv, re
from os import path, linesep
class deletelines(QgsProcessingAlgorithm):
Input = 'input_iist'
No_of_rows = 'no_of_rows'
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def deletelines(self):
return deletelines()
def name(self):
return 'Deletelines'.lower()
def displayName(self):
return self.tr('Delete_lines_from_CSV_TXT')
def group(self):
return self.tr('My_Scripts')
def groupId(self):
return 'myscripts1'
def shortHelpString(self):
return self.tr('Delete Lines from csv')
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFile(name=self.Input,description="Input CSV List",extension="csv(*.csv)"))
self.addParameter(QgsProcessingParameterNumber(self.No_of_rows,self.tr('Field length'), minValue=0, defaultValue=2))
and when I run this, I am getting
Don't know how to proceed further. In QGIS 2.18, I'd just paste the remaining code - as shown in the image below, highlighted in red and will get the outputs.
If I can learn how to implement this non-QGIS script within QGIS 3.4, can manage with the other non-QGIS, QGIS scripts.



