大家好!我正在编写一个小程序来使用 word 文件模板显示多边形角点的坐标(来自 SHP 文件)。在 word 文件中,我指定了程序数据应该去的字段 (jinia2)。此刻,我得到了坐标,形成了文件(word),但是:
我将不胜感激。
import os
import sys
import pyproj
from PySide6.QtWidgets import *
from docxtpl.template import DocxTemplate
# Для данных
import math
import geopandas
class QHLine(QFrame):
def __init__(self):
super(QHLine,self).__init__()
self.setFrameShape(QFrame.HLine)
self.setFrameShadow(QFrame.Sunken)
def dms_convert(self):
if len(str(self)) > 2:
dd = math.trunc(float(self))
mm = 60 * math.modf(float(self))[0]
if mm < 10:
mm2 = '0' + str(math.trunc(mm))
else:
mm2 = math.trunc(mm)
ss = round(60 * math.modf(mm)[0], 2)
if math.trunc(ss) < 10:
ss = '0' + '{:.2f}'.format(ss)
else:
ss = '{:.2f}'.format(ss)
stroka = f'{dd}{chr(176)}{mm2}{chr(8242)}{ss}{chr(8243)}'
else:
stroka = f'{self}{chr(176)}'
return stroka
class win_rtf(QMainWindow):
def __init__(self):
super(win_rtf, self).__init__()
self.setWindowTitle("Report generation")
self.cwd = os.getcwd()
self.setFixedSize(600,350)
grid = QGridLayout()
self.btn_open_shp = QPushButton('Open SHP', self)
self.btn_open_shp.clicked.connect(self.sgnl_open_shp)
self.name_lbl = QLabel('Name:',self)
self.name_shp_vision = QLineEdit()
self.sk_lbl = QLabel('CRS:', self)
self.shp_epsg_vision = QLineEdit()
self.type_survey = QLabel('Type survey:', self)
self.select_ts = QComboBox()
self.select_ts.addItems(['Type1', 'Type2', 'Type3'])
self.input_scale = QLabel('Scale 1:', self)
self.input_scale_vision = QLineEdit()
self.select_year = QLabel('Year survey:', self)
self.select_year_vision = QLineEdit()
self.company = QLabel('Company:', self)
self.company_vision = QComboBox()
self.company_vision.addItems(['Com1', 'Com2'])
self.btn_select_template = QPushButton('Select template', self)
self.btn_select_template.clicked.connect(self.sgnl_open_template)
self.lbl_template_docx = QLabel('Name:', self)
self.template_name_vision = QLineEdit()
self.btn_path_save = QPushButton('Save as...', self)
self.btn_path_save.clicked.connect(self.sgnl_path_save)
self.btn_path_save_vision = QLineEdit()
self.btn_run = QPushButton('Run', self)
self.btn_run.clicked.connect(self.sgnl_run_script)
grid.setSpacing(10)
grid.addWidget(self.btn_open_shp, 0, 0)
grid.addWidget(self.name_lbl, 0, 1)
grid.addWidget(self.name_shp_vision, 0, 2, 1, 4)
grid.addWidget(self.sk_lbl, 1, 1)
grid.addWidget(self.shp_epsg_vision, 1, 2, 1, 4)
grid.addWidget(QHLine(), 2, 0, 2, 5)
grid.addWidget(self.type_survey, 4, 0)
grid.addWidget(self.select_ts, 5, 0)
grid.addWidget(self.select_year, 4, 1)
grid.addWidget(self.select_year_vision, 5, 1)
grid.addWidget(self.input_scale, 4, 2)
grid.addWidget(self.input_scale_vision, 5, 2)
grid.addWidget(self.company, 4, 3)
grid.addWidget(self.company_vision, 5, 3)
grid.addWidget(QHLine(), 6, 0, 6, 5)
grid.addWidget(self.btn_select_template, 7, 0)
grid.addWidget(self.lbl_template_docx, 7, 1)
grid.addWidget(self.template_name_vision, 5, 2, 5, 3)
grid.addWidget(self.btn_path_save, 10, 0)
grid.addWidget(self.btn_path_save_vision, 7, 1, 7, 3)
grid.addWidget(self.btn_run, 11, 3)
widget = QWidget()
widget.setLayout(grid)
self.setMenuWidget(widget)
def sgnl_type_work(self):
self.select_tw = self.select_ts.currentText()
def sgnl_open_shp(self):
self.shp_open_file,_ = QFileDialog.getOpenFileName(self,
"Open file...",
self.cwd,
"SHP file (*.shp)")
self.name_shp_vision.setText(str(os.path.basename(self.shp_open_file)))
self.read_shp_gpd = geopandas.read_file(self.shp_open_file)
self.shp_epsg_vision.setText(str(self.read_shp_gpd.crs.to_authority())) # read CRS shp file
# Get meter coordinates of corners
self.point_NW_m = (self.read_shp_gpd.total_bounds[0], self.read_shp_gpd.total_bounds[3])
self.point_NE_m = (self.read_shp_gpd.total_bounds[2], self.read_shp_gpd.total_bounds[3])
self.point_SW_m = (self.read_shp_gpd.total_bounds[0], self.read_shp_gpd.total_bounds[1])
self.point_SE_m = (self.read_shp_gpd.total_bounds[2], self.read_shp_gpd.total_bounds[1])
#print(self.point_NW_m,'\n',self.point_NE_m,'\n', self.point_SW_m,'\n',self.point_SE_m)
to_wgs84 = pyproj.Transformer.from_crs(self.read_shp_gpd.crs, 4326)
# Getting Latitude/Longitude
self.point_NW_g = to_wgs84.transform(self.read_shp_gpd.total_bounds[0], self.read_shp_gpd.total_bounds[3])
self.point_NE_g = to_wgs84.transform(self.read_shp_gpd.total_bounds[2], self.read_shp_gpd.total_bounds[3])
self.point_SW_g = to_wgs84.transform(self.read_shp_gpd.total_bounds[0], self.read_shp_gpd.total_bounds[1])
self.point_SE_g = to_wgs84.transform(self.read_shp_gpd.total_bounds[2], self.read_shp_gpd.total_bounds[1])
def sgnl_open_template(self):
self.template_open_file, _ = QFileDialog.getOpenFileName(self,
"Select template...",
self.cwd,
"Template docx (*.docx)")
self.template_name_vision.setText(os.path.basename(self.template_open_file))
def sgnl_path_save(self):
self.dir_name = QFileDialog.getExistingDirectory(self,'Select path')
self.btn_path_save_vision.setText(str(self.dir_name))
def sgnl_run_script(self):
for i in range(0, len(self.read_shp_gpd)): #loop through all polygons for rectangular coordinates
template = DocxTemplate(self.template_open_file)
name = self.read_shp_gpd.loc[i, "name"]
Xmin = self.read_shp_gpd.total_bounds[0]
Ymin = self.read_shp_gpd.total_bounds[1]
Xmax = self.read_shp_gpd.total_bounds[2]
Ymax = self.read_shp_gpd.total_bounds[3]
point_NW_lat = dms_convert(self.point_NW_g[0])
point_NW_long = dms_convert(self.point_NW_g[1])
point_NE_lat = dms_convert(self.point_NE_g[0])
point_NE_long = dms_convert(self.point_NE_g[1])
point_SW_lat = dms_convert(self.point_SW_g[0])
point_SW_long = dms_convert(self.point_SW_g[1])
point_SE_lat = dms_convert(self.point_SE_g[0])
point_SE_long = dms_convert(self.point_SE_g[1])
context = {
'title': str('Form ' + ' ' + name),
'year': self.select_year_vision,
'type_work': self.select_ts,
'scale': self.input_scale_vision,
'company': self.company_vision,
'minx': Xmin,
'miny': Ymin,
'maxx': Xmax,
'maxy': Ymax,
'NW_lat': point_NW_lat + ' N',
'NW_long': point_NW_long + ' E',
'NE_lat': point_NE_lat + ' N',
'NE_long': point_NE_long + ' E',
'SW_lat': point_SW_lat + ' N',
'SW_long': point_SW_long + ' E',
'SE_lat': point_SE_lat + ' N',
'SE_long': point_SE_long + ' E',
}
template.render(context)
template.save(self.dir_name + 'Form № ' + name + '.docx')
app = QApplication(sys.argv)
w = win_rtf()
w.show()
app.exec()
type here
正在形成一个word文件,但有我无法修复的错误