管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-07 5429333e590537ff241e442cedd40b2a5de9c7dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from qgis.core import *
from qgis.gui import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtXml import *
 
QgsApplication.setPrefixPath(r"C:/Program Files/QGIS 3.16/bin", True)
qgs = QgsApplication([], True)
qgs.initQgis()
 
project = QgsProject.instance()
project.read(r'E:/terrait/TianJin/LFServer/QGIS/Test.qgz')
print("FileName: " + project.fileName())
 
# layers = project.mapLayers().values()
# root = project.layerTreeRoot()
 
# layout = QgsPrintLayout(project)
layout = QgsLayout(project)
layout.initializeDefaults()
 
with open(r"E:/terrait/TianJin/LFServer/QGIS/Template.qpt", 'r', encoding='utf-8') as f:
    template_content = f.read()
doc = QDomDocument()
doc.setContent(template_content)
layout.loadFromTemplate(doc, QgsReadWriteContext(), False)
 
# map = QgsLayoutItemMap(layout)
# map.zoomToExtent(layers[0].extent())
# layout.addItem(map)
 
# collection = layout.pageCollection()
# page = QgsLayoutItemPage(layout)
# page.setPageSize('A4')
# collection.addPage(page)
 
# layoutView = QgsLayoutView()
# layoutView.setPreviewModeEnabled(True)
# layoutView.setContentsMargins(0, 0, 0, 0)
# layoutView.setCurrentLayout(layout)
 
exporter = QgsLayoutExporter(layout)
img_path = os.path.join(r"E:/terrait/TianJin/LFServer/QGIS", "render.png")
exporter.exportToImage(img_path, QgsLayoutExporter.ImageExportSettings())
 
qgs.exitQgis()