Your code has several issues. First one, 'read_excel' method belongs pandas library and this is not reflected in your question. Additionally, you need to define how many features has your shapefile.POLYLINE. Assuming a polyline with two features, I created a xls file named NI.xls with field code and only two records. So, I also need to define points for each feature:
[-40.5, -20.666],[-39.849998, -18.700001]
[-39.849998, -18.700001],[-39.816002, -19.6]
For avoiding projection issues, I assumed the data for EPSG 4326.
So, with all above assumptions, my code looks as follow:
import shapefile
import pandas as pd
df = pd.read_excel("NI.xls")
df.head()
w = shapefile.Writer(shapefile.POLYLINE)
w.line(parts=[[[-40.5, -20.666],[-39.849998, -18.700001]]])
w.poly(parts=[[[-39.849998, -18.700001],[-39.816002, -19.6]]], shapeType=shapefile.POLYLINE)
w.field('code','C','40')
for i, row in df.iterrows():
w.record(str(row[0]))
filename = "/home/zeito/pyqgis_data/routeNI7"
w.save(filename)
prj = open("%s.prj" % filename, "w")
epsg = 'GEOGCS["WGS 84",'
epsg += 'DATUM["WGS_1984",'
epsg += 'SPHEROID["WGS 84",6378137,298.257223563]]'
epsg += ',PRIMEM["Greenwich",0],'
epsg += 'UNIT["degree",0.0174532925199433]]'
prj.write(epsg)
prj.close()
After running it at Python Console, I got expected result:
Editing Note:
By using osr osgeo python module, it is easier to define shapefile projection from EPSG:
import shapefile
import pandas as pd
from osgeo import osr
df = pd.read_excel("NI.xls")
df.head()
w = shapefile.Writer(shapefile.POLYLINE)
w.line(parts=[[[-40.5, -20.666],[-39.849998, -18.700001]]])
w.poly(parts=[[[-39.849998, -18.700001],[-39.816002, -19.6]]], shapeType=shapefile.POLYLINE)
w.field('code','C','40')
for i, row in df.iterrows():
w.record(str(row[0]))
filename = "/home/zeito/pyqgis_data/routeNI7"
w.save(filename)
add_prj = open("%s.prj" % filename, "w")
proj = osr.SpatialReference()
proj.ImportFromEPSG(4326)
epsg = proj.ExportToWkt()
add_prj.write(epsg)
add_prj.close()