[Qgis-user] Re: new xytools plugin: now open and save simple Excel files

gene martin.laloux at gmail.com
Fri Dec 16 01:37:05 PST 2011


As a complement, my script, "Convert a Microsoft Excel file with X,Y
coordinates to a shapefile (or a memory layer)  (
http://osgeo-org.1803224.n2.nabble.com/new-xytools-plugin-now-open-and-save-simple-Excel-files-td7098439.html#a7099937
http://osgeo-org.1803224.n2.nabble.com/new-xytools-plugin-now-open-and-save-simple-Excel-files-td7098439.html#a7099937
, allows the direct creation of a shapefile (for points)

import xlrd
from PyQt4.QtCore import * 
wb = xlrd.open_workbook('testxy.xls')

# selection of first sheet
sh = wb.sheet_by_index(0) # or by_name)
# data in first sheet (x,y points)
for rownum in range(sh.nrows):
     print sh.row_values(rownum)
[u'id', u'x', u'y', u'test']
[1.0, 235.0, 424.0, u'a']
[2.0, 245.0, 444.0, u'b']
[3.0, 255.0, 464.0, u'c']
[4.0, 265.0, 484.0, u'd']
[5.0, 275.0, 504.0, u'e']
[6.0, 285.0, 524.0, u'f']
[7.0, 295.0, 544.0, u'g']

# creation of the shapefile
fields ={0 : QgsField("id", QVariant.Int),1 : QgsField("test",
QVariant.String) }
writer = QgsVectorFileWriter("testxy.shp", "CP1250", fields, QGis.WKBPoint,
None, "ESRI Shapefile")

# add x,y points from Excel spreadsheet
for rownum in range(sh.nrows):
    if rownum == 0 : # labels !
          continue
    else:
          id = sh.row_values(rownum)[0]
          x = sh.row_values(rownum)[1]
          y = sh.row_values(rownum)[2]
          test = sh.row_values(rownum)[3]
          fet = QgsFeature()
          fet.addAttribute(0, QVariant(id))
          fet.addAttribute(1, QVariant(test))
          writer.addFeature(fet)
del writer


--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/new-xytools-plugin-now-open-and-save-simple-Excel-files-tp7098439p7100155.html
Sent from the qgis-user mailing list archive at Nabble.com.



More information about the Qgis-user mailing list