NOTE: THIS IS FOR QGIS 2.14.1 and uses VectorGrid instead of CreateGrid also though the question How to generate a grid programmatically in Python without GUI? is answered fantastically the PyQGIS language is outdated
We have a .txt file "Extent.txt", in this example this file contains the coordinates of 4 points formatted like so;
Point,Lat,Lon
TL,-29.788254, 30.820720
TR,-29.788254, 30.825372
BL,-29.790710, 30.820720
BR,-29.790710, 30.825372
We want to add this shapefile in our QGIS Map Document, and then create a vector grid that has the same extents of this .txt file, all using PyQGIS. We add the .txt file with a PyQGIS script
import csv, ctypes, os, sys, datetime, string
import processing
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from qgis.core import *
from qgis.utils import iface
#Load CSV
gridSettings = QSettings()
gridoldValidation = gridSettings.value( "/Projections/defaultBehaviour")
gridSettings.setValue( "/Projections/defaultBehaviour", "useGlobal" )
gridInFlnm='Extent.txt'
gridInDrPth='PATH_TO_CSV_FOLDER'
gridInFlPth="file:///"+gridInDrPth+gridInFlnm
griduri = gridInFlPth+"?delimiter=%s&xField=%s&yField=%s" % (",","lon","lat")
gridbh = QgsVectorLayer(griduri, gridInFlnm, "delimitedtext")
gridbh.isValid()
gridbh.setCrs(QgsCoordinateReferenceSystem(32365, QgsCoordinateReferenceSystem.EpsgCrsId))
QgsMapLayerRegistry.instance().addMapLayer(gridbh)
gridSettings.setValue( "/Projections/defaultBehaviour", gridoldValidation )
This produces our point vector layer from our .txt file!

So...How do we get our Vector Grid?
NOTE: THIS IS FOR QGIS 2.14.1 and uses VectorGrid instead of CreateGrid also though the question How to generate a grid programmatically in Python without GUI? is answered fantastically the PyQGIS language is outdated
