[Python] 纯文本查看 复制代码
# -*- coding:cp936 -*-
import xlrd
import arcpy
import os
def main():
# 输入表格
xl0 = arcpy.GetParameterAsText(0)
# 输出数据库
out_folder = arcpy.GetParameterAsText(1)
out_name = arcpy.GetParameterAsText(2)
# 打开表
table0 = xlrd.open_workbook(xl0)
features = table0.sheets()[0] # 图层列表
arcpy.CreateFileGDB_management(out_folder, out_name)
for i in range(1, features.nrows):
(xh0, ysbm0, jhtz0, ysmc0, ystj0, bz0) = features.row_values(i)[:6] # 序号-图层名称(要素别名)-几何特征-属性表名(要素名称)-约束条件-备注
ysmc0 = ysmc0.encode('unicode-escape').decode('string_escape')
jhtz0 = jhtz0.encode('unicode-escape').decode('string_escape')
sjkdz0 = os.path.join(out_folder, out_name + ".gdb") # 数据库地址
ysdz0 = os.path.join(sjkdz0, ysmc0) # 要素地址
arcpy.CreateFeatureclass_management(sjkdz0, ysmc0, jhtz0) # 创建要素
arcpy.AlterAliasName(ysdz0, ysbm0)
arcpy.AddMessage(u"要素 " + ysbm0 + u" 创建成功")
fields = table0.sheet_by_name(ysmc0) # 字段列表
for j in range(1, fields.nrows): # 获取字段列表工作表每行的字段属性
(xh, zdmc, zddm, zdlx, zdcd, xsws, yz, ystj) = fields.row_values(j)[
:8] # 序号-字段名称(字段别名)-字段代码-字段类型-字段长度-小数位数-值域-约束条件
zddm = zddm.encode('unicode-escape').decode('string_escape')
zdlx = zdlx.encode('unicode-escape').decode('string_escape')
zdcd = str(int(zdcd))
ystj = ystj.encode('unicode-escape').decode('string_escape')
if ystj == "M":
if zdlx == "TEXT":
arcpy.AddField_management(ysdz0, zddm, zdlx, "", "", zdcd, zdmc, "NON_NULLABLE", "", "")
else:
arcpy.AddField_management(ysdz0, zddm, zdlx, "", "", "", zdmc, "NON_NULLABLE", "", "")
else:
if zdlx == "TEXT":
arcpy.AddField_management(ysdz0, zddm, zdlx, "", "", zdcd, zdmc, "NULLABLE", "", "")
else:
arcpy.AddField_management(ysdz0, zddm, zdlx, "", "", "", zdmc, "NULLABLE", "", "")
arcpy.AddMessage(u"字段 " + zddm + u" 添加完成")
if __name__ == '__main__':
main()