Here is the working code using QGIS 3.16.8
1- Enable Qgis macro
Settings>>Options>>General>>(Enable Macros Always)
2- Create a batch file (runQGIS.bat) and paste this
"C:\Program Files\QGIS 3.16.8\bin\qgis-ltr-bin.exe" --code "c:\Users\me\Desktop\xyz\utm.py"
3- Or create a new shortcut to (qgis-ltr-bin.exe) then
right-click >> properties>> in target paste this
"C:\Program Files\QGIS 3.16.8\bin\qgis-ltr-bin.exe" --code c:\Users\me\Desktop\xyz\utm.py
4- Crate folder xyz in me Desktop and put your CSV file in this format
y,x,z
22.956425942,11.708518637,92.7
22.256725748,11.508518344,99.6
5- Paste the code into utm.py and save in the same xyz folder
6- You can delete the last line in the code if you do not want to auto-close Qgis
7- Warning You have to choose the right resultant file from 5 zones
Take care this code does not work if your points fall in 2 zones or out of the 5 zones.
You have to modify this code to change change to path of windows user Desktop/xyz and aqgis-ltr-bin.exe path and your county CRS (5 zones max)
################################################
#---------- You May Change This -----------#
################################################
your_working_folder="xyz"
your_working_file="1.csv"
in_epsg="EPSG:4326"
################################################
my_crs1="EPSG:32630"
my_crs2="EPSG:32631"
my_crs3="EPSG:32632"
my_crs4="EPSG:32633"
my_crs5="EPSG:32634"
################################################
import os
import processing
from qgis.core import *
from qgis.utils import iface
import platform
################################################
userhome = os.path.expanduser('~')
in_path ='file:/'+userhome + '/Desktop/'+ your_working_folder +'/'
out_path =userhome + '/Desktop/'+ your_working_folder +'/'
################################################
in_all=in_path+your_working_file
f= '{}?type=csv&xField={}&yField={}&zField={}&crs='+ in_epsg
uri = f.format(in_all, 'x','y','z','q','sat')
#(q= GPS quality from PPK RTK correction) (sat= number of satellites)
iface.addVectorLayer(uri, your_working_file, 'delimitedtext')
activelayer = iface.activeLayer()
#################################################
cut_this = "EPSG:"
#################################################
my_layer_name= my_crs1.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ ( " +my_crs1+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs1,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+" "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
#################################################
my_layer_name= my_crs2.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ ( " +my_crs2+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs2,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+" "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs3.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ ( " +my_crs3+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs3,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+" "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs4.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ ( " +my_crs4+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs4,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+" "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs5.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ ( " +my_crs5+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs5,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+" "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
#iface.actionExit().trigger()
os._exit(0)