Thursday 23 April 2015

python - Reading Excel sheet in ArcPy script?



I'm writing a python code to convert X,Y points in excel to shapefile. In this process i have toread xy points from shhet1. What is the step i have to include in my process to read data from sheet1 of an excel work book(97-2003).
I wrote a code as follows..


import arcpy  
from arcpy import env
import Xlrd
env.workspace="E:\"
input_table="123.xls\Sheet1"

But its not working.



Answer




It doesn't work because you haven't called the Xlrd modules to read the Excel spreadsheet. Implement it something like this:


import xlrd
workbook = xlrd.open_workbook('my_workbook.xls')
worksheet = workbook.sheet_by_name('Sheet1')

This will allow you to read an XLS file with Python. However, ArcPy will read XLS without Xlrd. You can consider the Excel workbook to be a workspace containing potentially many tables (worksheets). So you could do something like:


arcpy.env.workspace = r'E:\123.xls'
input_table = 'Sheet1$'

...or cut to the chase with:



arcpy.MakeXYEventLayer_management(r'E:\123.xls\Sheet1$',lat,long,layername,SpatialRef)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...