您现在的位置是:首页 >学无止境 >python 根据gdb省库提取属性,输出房地一体申请表网站首页学无止境

python 根据gdb省库提取属性,输出房地一体申请表

Darling~R 2024-06-02 00:00:02
简介python 根据gdb省库提取属性,输出房地一体申请表

python 根据gdb省库提取属性,输出房地一体申请表

-- coding: utf-8 --

import win32com.client
from PyQt5.QtWidgets import QApplication
import traceback, threading
from PyQt5.QtWidgets import QMessageBox
import os, sys, subprocess
import sys, shapefile, traceback
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QAction
from PyQt5 import QtCore, QtGui, QtWidgets
from osgeo import ogr
import os, sys, glob
def mab_ora(mdbpath):
conn = win32com.client.Dispatch(r"ADODB.Connection")
DSN = ‘PROVIDER = Microsoft.Jet.OLEDB.4.0;DATA SOURCE = %s’ % mdbpath
conn.Open(DSN)
return conn

def resource_path(relative_path):
if getattr(sys, ‘frozen’, False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(“.”)
return os.path.join(base_path, relative_path)

def select_zrz(path_zrz, path_zjd, path_jsyd, zddm_xz):
# coding=utf-8

ds = ogr.Open(path_zrz, 0)
layer = ds.GetLayer()
# featuredefn = layer.GetLayerDefn()  # 获取图层属性表定义
# fieldcount = featuredefn.GetFieldCount()  # 获取属性表中字段数

# for attr in range(fieldcount):
#     fielddefn = featuredefn.GetFieldDefn(attr)
#     print("%s:  %s" % ( fielddefn.GetNameRef(),fielddefn.GetFieldTypeName(fielddefn.GetType())))
#outtxtfile = open('JZX1.txt', 'w')  # 以可写方式打开
feature = layer.GetNextFeature()  # 遍历
list_zrz = {}
while feature:
    # 读取字段值
    ZDDM = feature.GetFieldAsString('ZDDM')
    if zddm_xz in ZDDM:
        BDCDYH = feature.GetFieldAsString('BDCDYH')
        FWZL = feature.GetFieldAsString('FWZL')
        JZMJ = feature.GetFieldAsString('JZMJ')
        FWJG = feature.GetFieldAsString('FWJG')
        QTGSD = feature.GetFieldAsString('QTGSD')
        QTGSN = feature.GetFieldAsString('QTGSN')
        QTGSX = feature.GetFieldAsString('QTGSX')
        QTGSB = feature.GetFieldAsString('QTGSB')
        DCY = feature.GetFieldAsString('DCY')
        DCRQ = feature.GetFieldAsString('DCRQ')
        CLR = feature.GetFieldAsString('CLR')
        CLRQ = feature.GetFieldAsString('CLRQ')
        FWJGRQ = feature.GetFieldAsString('FWJGRQ')
        ZZDMJ = feature.GetFieldAsString('ZZDMJ')
        ZCS = feature.GetFieldAsString('ZCS')
        list_zrz.setdefault(ZDDM, []).append(BDCDYH)
        list_zrz.setdefault(ZDDM, []).append(FWZL)
        list_zrz.setdefault(ZDDM, []).append(JZMJ)
        list_zrz.setdefault(ZDDM, []).append(FWJG)
        list_zrz.setdefault(ZDDM, []).append(QTGSD)
        list_zrz.setdefault(ZDDM, []).append(QTGSN)
        list_zrz.setdefault(ZDDM, []).append(QTGSX)
        list_zrz.setdefault(ZDDM, []).append(QTGSB)
        list_zrz.setdefault(ZDDM, []).append(DCY)
        list_zrz.setdefault(ZDDM, []).append(DCRQ)
        list_zrz.setdefault(ZDDM, []).append(CLR)
        list_zrz.setdefault(ZDDM, []).append(CLRQ)
        list_zrz.setdefault(ZDDM, []).append(FWJGRQ)
        list_zrz.setdefault(ZDDM, []).append(ZZDMJ)
        list_zrz.setdefault(ZDDM, []).append(ZCS)
    # 清除缓存并获取下一个要素
    feature.Destroy()
    feature = layer.GetNextFeature()
# 清除DataSource缓存并关闭TXT文件
ds.Destroy()
ds = ogr.Open(path_zjd, 0)
layer = ds.GetLayer()
feature = layer.GetNextFeature()
while feature:
    ZDMJ = feature.GetFieldAsString('ZDMJ')
    ZDDM = feature.GetFieldAsString('ZDDM')
    pzmj = feature.GetFieldAsString('PZMJ')
    if zddm_xz in ZDDM:
        list_zrz.setdefault(ZDDM, []).append(ZDMJ)
        list_zrz.setdefault(ZDDM, []).append(pzmj)
    # 清除缓存并获取下一个要素
    feature.Destroy()
    feature = layer.GetNextFeature()
    # 清除DataSource缓存并关闭TXT文件
ds.Destroy()
#建设用地
ds = ogr.Open(path_jsyd, 0)
layer = ds.GetLayer()
feature = layer.GetNextFeature()
while feature:
    ZDMJ = feature.GetFieldAsString('ZDMJ')
    ZDDM = feature.GetFieldAsString('ZDDM')
    pzmj = feature.GetFieldAsString('PZMJ')
    if zddm_xz in ZDDM:
        list_zrz.setdefault(ZDDM, []).append(ZDMJ)
        list_zrz.setdefault(ZDDM, []).append(pzmj)
    # 清除缓存并获取下一个要素
    feature.Destroy()
    feature = layer.GetNextFeature()
    # 清除DataSource缓存并关闭TXT文件
ds.Destroy()
return list_zrz

def get_str(text):
if text in(‘’, ‘None’, None):
text = ’ ’
return text

def select_qlr(conn, zddm_xz):
list_qlr = {}
sql = “”“SELECT BDCDYH, QLRMC, QLRLX, QLRZJZL, QLRZJH, XB, DZ, GYQK, QLRLXDH FROM QLR WHERE BDCDYH LIKE ‘%%%s%%’
“””%zddm_xz
rs = win32com.client.Dispatch(r’ADODB.Recordset’)
rs.Open(sql, conn, 1)
if rs.recordcount > 0:
rs.MoveFirst() # 光标移到首条记录
while True:
if rs.EOF:
break
else:
list1 = []
BDCDYH = get_str(str(rs.Fields[0].Value))
list1.append(get_str(str(rs.Fields[1].Value)))
list1.append(get_qlrlx(get_str(str(rs.Fields[2].Value))))
list1.append(get_zjzl(str(rs.Fields[3].Value)))
list1.append(get_str(str(rs.Fields[4].Value)))
list1.append(get_xb(str(rs.Fields[5].Value)))
list1.append(get_str(str(rs.Fields[6].Value)))
list1.append(get_str(str(rs.Fields[7].Value)))
list1.append(get_str(str(rs.Fields[8].Value)))
list_qlr.setdefault(BDCDYH, []).append(list1)
rs.MoveNext()
rs.Close()
return list_qlr

def get_xb(text):
if text == ‘1’:
return ‘男’
elif text == ‘2’:
return ‘女’
else:
return ‘其他’

def get_qlrlx(text):
if text == ‘1’:
return ‘个人’
else:
return ‘其他’

def get_zjzl(text):
if text == ‘1’:
return ‘身份证’
else:
return ‘其他’

def get_qt(text):
list = {‘1’:‘自有墙’, ‘2’:‘共有墙’, ‘3’:‘借墙’}
try:
return list[text]
except:
return ‘/’

def get_fwjg(text):
list = {‘1’:‘钢结构’, ‘2’:‘钢和钢筋混凝土结构’, ‘3’:‘钢筋混凝土结构’, ‘4’:‘混合结构’, ‘5’:‘砖木结构’, ‘6’:‘其他结构’}
try:
return list[text]
except:
return ‘/’

def get_date(text):
list_text = str(text).split(‘/’)
return str(‘%s年%s月%s日’%(list_text[0], list_text[1].zfill(2), list_text[2].zfill(2)))

def output_log1(message, outpath):
with open(outpath, ‘a’) as f:
f.write(message)

def output_word(path_sk, path_bc, self):
self.textEdit.append(‘开始运行···’)
QApplication.processEvents()
zddm_xz = self.lineEdit_3.text()
path_mdb = glob.glob(‘%s数据库成果非空间数据*SXB.mdb’%path_sk)[0]
path_zrz = glob.glob(‘%s数据库成果空间数据*ZRZ.shp’%path_sk)[0]
path_zd = glob.glob(‘%s数据库成果空间数据*ZJD.shp’%path_sk)[0]
path_jsyd = glob.glob(‘%s数据库成果空间数据*SHYQZD_DB.shp’%path_sk)[0]
outpath = os.path.join(path_bc, ‘错误记录.txt’)
conn = mab_ora(path_mdb)
self.textEdit.append(‘正在读取空间图形信息···’)
QApplication.processEvents()
list_zrz = select_zrz(path_zrz, path_zd, path_jsyd, zddm_xz)
self.textEdit.append(‘正在读取权利人信息···’)
QApplication.processEvents()
list_qlr = select_qlr(conn, zddm_xz)
jd_pj = 100 / len(list_zrz)
jd = 100 / len(list_zrz)
self.textEdit.append(‘开始输出申请表’)
QApplication.processEvents()
tem = 1
for zddm in list_zrz:
try:
print(zddm)
bdcdyh = list_zrz[zddm][0]
fwzl = list_zrz[zddm][1]
jzmj = list_zrz[zddm][2]
fwjg = get_fwjg(list_zrz[zddm][3])
qtd = get_qt(list_zrz[zddm][4])
qtn = get_qt(list_zrz[zddm][5])
qtx = get_qt(list_zrz[zddm][6])
qtb = get_qt(list_zrz[zddm][7])
dcy = list_zrz[zddm][8]
dcrq = get_date(list_zrz[zddm][9])
clr = list_zrz[zddm][10]
clrq = get_date(list_zrz[zddm][11])
fwjgrq = get_date(list_zrz[zddm][12])
zzdmj = list_zrz[zddm][13]
zcs = str(int(float(list_zrz[zddm][14])))
zdmj = list_zrz[zddm][15]
pzmj = list_zrz[zddm][16]
list_qlxx = list_qlr[bdcdyh[:27] + ‘1’]
i = 0
gyqkxx = []
for qlxx in list_qlxx:
if i == 0:
qlrmc = qlxx[0]
qlrlx = qlxx[1]
zjzl = qlxx[2]
zjh = qlxx[3]
xb = get_xb(qlxx[4])
dhhm = qlxx[7]
else:
qlxx[5] = fwzl
gyqkxx.append(‘/’.join(qlxx[:-1]))
i += 1
print(list_qlxx)
word = win32com.client.DispatchEx(‘Word.Application’)
word.Visible = False
word.DisplayAlerts = False
print(str(pzmj))
if qlrlx == ‘个人’ and str(pzmj) == ‘0.00’:
path_doc_dcb = os.path.join(os.getcwd(), resource_path(os.path.join(“temp”, “01不动产调查登记申请表.doc”)))
elif qlrlx == ‘个人’ and str(pzmj) != ‘0.00’:
path_doc_dcb = os.path.join(os.getcwd(), resource_path(os.path.join(“temp”, “01不动产调查登记申请表-pz.doc”)))
elif pzmj != ‘’:
path_doc_dcb = os.path.join(os.getcwd(), resource_path(os.path.join(“temp”, “01不动产调查登记申请表-qt-pz.doc”)))
else:
path_doc_dcb = os.path.join(os.getcwd(), resource_path(os.path.join(“temp”, “01不动产调查登记申请表-qt.doc”)))
doc = word.Documents.Open(FileName=path_doc_dcb)
if str(pzmj) != ‘0.00’:
word.Selection.Find.Execute(‘PZMJ’, False, False, False, False, False, True, 1, True, pzmj, 2)
word.Selection.Find.Execute(‘ZDYBH’, False, False, False, False, False, True, 1, True, zddm[-7:], 2)
word.Selection.Find.Execute(‘ZDDM’, False, False, False, False, False, True, 1, True, zddm, 2)
word.Selection.Find.Execute(‘BDCDYH’, False, False, False, False, False, True, 1, True, bdcdyh[:27] + ‘1’, 2)
word.Selection.Find.Execute(‘QLRMC’, False, False, False, False, False, True, 1, True, qlrmc, 2)
#word.Selection.Find.Execute(‘QLRLX’, False, False, False, False, False, True, 1, True, qlrlx, 2)
#word.Selection.Find.Execute(‘ZJZL’, False, False, False, False, False, True, 1, True, zjzl, 2)
word.Selection.Find.Execute(‘SFZHM’, False, False, False, False, False, True, 1, True, zjh, 2)
word.Selection.Find.Execute(‘DHHM’, False, False, False, False, False, True, 1, True, dhhm, 2)
word.Selection.Find.Execute(‘ZDZL’, False, False, False, False, False, True, 1, True, fwzl, 2)
word.Selection.Find.Execute(‘JGSJ’, False, False, False, False, False, True, 1, True, fwjgrq, 2)
word.Selection.Find.Execute(‘FWJG’, False, False, False, False, False, True, 1, True, fwjg, 2)

        if gyqkxx:
            table = doc.Tables(1)
            table.Cell(17, 2).Range.Text = '
'.join(gyqkxx)
        else:
            word.Selection.Find.Execute('GYQLR', False, False, False, False, False, True, 1, True, '/', 2)
        word.Selection.Find.Execute('ZCS', False, False, False, False, False, True, 1, True, zcs, 2)
        if zcs == '1':
            szc = '1'
        else:
            szc = '1-%s'%zcs
        word.Selection.Find.Execute('SZC', False, False, False, False, False, True, 1, True, szc, 2)
        word.Selection.Find.Execute('ZZDMJ', False, False, False, False, False, True, 1, True, zzdmj, 2)
        word.Selection.Find.Execute('ZDMJ', False, False, False, False, False, True, 1, True, zdmj, 2)
        word.Selection.Find.Execute('JZMJ', False, False, False, False, False, True, 1, True, jzmj, 2)
        word.Selection.Find.Execute('QTD', False, False, False, False, False, True, 1, True, qtd, 2)
        word.Selection.Find.Execute('QTN', False, False, False, False, False, True, 1, True, qtn, 2)
        word.Selection.Find.Execute('QTX', False, False, False, False, False, True, 1, True, qtx, 2)
        word.Selection.Find.Execute('QTB', False, False, False, False, False, True, 1, True, qtb, 2)
        word.Selection.Find.Execute('DCR', False, False, False, False, False, True, 1, True, dcy, 2)
        word.Selection.Find.Execute('DCCRQ', False, False, False, False, False, True, 1, True, dcrq, 2)
        word.Selection.Find.Execute('CLRQ', False, False, False, False, False, True, 1, True, clrq, 2)
        word.Selection.Find.Execute('CLR', False, False, False, False, False, True, 1, True, clr, 2)
        doc.SaveAs(os.path.join(path_bc, '%s-%s.doc'%(zddm, qlrmc)))
        #doc.Close()
        word.Quit()
        self.progressBar.setProperty("value", jd)
        self.textEdit.append('%s-成功' % zddm)
        QApplication.processEvents()
        jd = jd + jd_pj

    except:
        self.textEdit.append('%s-输出失败'%zddm)
        QApplication.processEvents()
        output_log1('%s-%s
'%(zddm, traceback.format_exc()), outpath)
        #doc.Close()
        word.Quit()
        tem = 2
if QMessageBox.information(None, '提示', "输出完成,是否打开文件夹进行查看",
                           QMessageBox.Yes | QMessageBox.No) == 16384:
    os.startfile(path_bc)
if tem == 2:
    subprocess.Popen(['start', outpath], shell=True)

def main(self):
def askdirectory_pdfhb_xz():
“”“”“”
_translate = QtCore.QCoreApplication.translate
folder = QFileDialog.getExistingDirectory()
self.lineEdit.setText(_translate(“Form”, folder))

def askdirectory_pdfhb_bc():
    """"""
    _translate = QtCore.QCoreApplication.translate
    folder = QFileDialog.getExistingDirectory()
    self.lineEdit_2.setText(_translate("Form", folder))

def ou_sqb():
    self.pushButton.setEnabled(False)
    path_sk = self.lineEdit.text()
    path_bc = self.lineEdit_2.text()
    output_word(path_sk, path_bc, self)
    self.pushButton.setEnabled(True)

def ye_main():
    try:

        td2 = threading.Thread(target=ou_sqb)
        # 设置线程为守护线程,防止退出主线程时,子线程仍在运行
        td2.setDaemon(True)
        td2.start()

    except:
        self.textEdit.append('出错了
%s' % traceback.format_exc())

self.toolButton.clicked.connect(askdirectory_pdfhb_xz)
self.toolButton_2.clicked.connect(askdirectory_pdfhb_bc)
self.pushButton.clicked.connect(ye_main)

if name == ‘main’:
import jm
app = QApplication(sys.argv)
MainWindow = QMainWindow() # QMainWindow模块名
self = jm.Ui_MainWindow()
self.setupUi(MainWindow)
main(self)

MainWindow.show()
sys.exit(app.exec_())
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。