基于python的读取excel转shp文件
需要对其中的版本注意一下,否则无法运行
// An highlighted block
import os
import xlrd
import shapefile#需要pip install pyshp
path = "02" #文件夹目录
files= os.listdir(path) #得到文件夹下的所有文件名称
def Write(shp,arr):
shp = shapefile.Writer(shp, shapeType=shapefile.POINT)
# 字段类型:此列索引处的数据类型。类型可以是:
# “C”:字符,文字。
# “N”:数字,带或不带小数。
# “F”:浮动(与“N”相同)。
# “L”:逻辑,表示布尔值True / False值。
# “D”:日期。
# “M”:备忘录,在GIS中没有意义,而是xbase规范的一部分。
shp.field("Elevation", "N", 10, 4)
shp.field("lat", "N", 10, 4)
shp.field("lon", "N", 10, 4)
shp.field("time", "D", 10, 4)
shp.field("gdHt", "N", 10, 4)
shp.field("height", "N", 10, 4)
for i in range(1, arr.nrows):
shp.point(arr.cell_value(i,1),arr.cell_value(i,2))
shp.record(arr.cell_value(i, 0),arr.cell_value(i,1)
,arr.cell_value(i,2),arr.cell_value(i,3),arr.cell_value(i,4),arr.cell_value(i,5))
shp.balance()
shp.close()
def read_excel_xls(path,file):#新版xlrd不支持xlsx,需要安装旧版1.2.0
workbook = xlrd.open_workbook(path) # 打开工作簿
sheets = workbook.sheet_names() # 获取工作簿中的所有表格
worksheet = workbook.sheet_by_name(sheets[0]) # 获取工作簿中所有表格中的的第一个表格
str=os.path.splitext(file)[0]
outputpath = "outputshp"+"/"+str + ".shp"#输出目录
Write(outputpath,worksheet)
for file in files: #遍历文件夹
pathall={}
if file.split('.')[-1] == 'xlsx' or file.split('.')[-1] == 'xls':
pathall=str(path+"/"+file)
read_excel_xls(pathall,file)
print("successed!")