def SaveTableImage(table): pixmap = table.grab() pixmap.save("widget.png") SaveTableImage(table) nrows = table.rowCount() ncols = table.columnCount() doc = QTextDocument() cursor = QTextCursor(doc) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(0) tableFormat.setCellSpacing(0) tableFormat.setBorder(1) tableFormat.setBorderBrush(QBrush(Qt.SolidPattern)) tableFormat.clearColumnWidthConstraints() textTable = cursor.insertTable(nrows + 1, ncols, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setBackground(QColor("#DADADA")) for i in range(ncols): cell = textTable.cellAt(0, i) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText(table.horizontalHeaderItem(i).text()) for i in range(nrows): for j in range(ncols): item = table.item(i, j) t = "" if item is None else str(item.text()) # if item.text().iEmpty(): # table.setItem(i,j,QTableWidgetItem("0")) cell = textTable.cellAt(i + 1, j) cellCursor = cell.firstCursorPosition() cellCursor.insertText(t) cursor.movePosition(QTextCursor.End) printer = QPrinter(QPrinter.PrinterResolution) printer.setPaperSize(QPrinter.A4) printer.setOrientation(QPrinter.Landscape) printer.setOutputFileName("w8.pdf") doc.setDocumentMargin(0) doc.setTextWidth(5) doc.print(printer)
def CreatePDF(self, out, timestamp, data, frame, rows, columns, fileName, VManager): ''' Create PDF QgsTask ''' QCoreApplication.processEvents() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) font_normal = QFont("Helvetica", 10, QFont.Normal) font_bold = QFont("Helvetica", 12, QFont.Bold) printer = QPrinter(QPrinter.HighResolution) printer.setOrientation(QPrinter.Portrait) printer.setOutputFormat(QPrinter.PdfFormat) printer.setFullPage(True) printer.setPaperSize(QPrinter.A4) printer.setOutputFileName(out) printer.setPageMargins(15, 15, 15, 15, QPrinter.Point) printer.setColorMode(QPrinter.Color) document = QTextDocument() document.setDefaultFont(font_normal) cursor = QTextCursor(document) cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) cursor.insertHtml(""" <p style='text-align: center;'> <img style='display: block; margin-left: auto; margin-right: auto;' src=\':/imgFMV/images/header_logo.png\' width='200' height='25' /> </p> <p style='text-align: center;'> <strong>Video : </strong>%s<strong> </p> <p style='text-align: center;'> <strong>TimeStamp : </strong>%s</p><br><br> """ % (fileName, timestamp)) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(2) tableFormat.setCellSpacing(2) centerFormat = QTextBlockFormat() centerFormat.setAlignment(Qt.AlignCenter) cursor.insertBlock(centerFormat) textTable = cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setFont(font_bold) tableHeaderFormat.setBackground(QColor("#67b03a")) tableHeaderFormat.setForeground(Qt.white) alternate_background = QTextCharFormat() alternate_background.setBackground(QColor("#DDE9ED")) for column in range(columns): cell = textTable.cellAt(0, column) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText(VManager.horizontalHeaderItem(column).text()) row = 0 for key in sorted(data.keys()): cell0 = textTable.cellAt(row + 1, 0) cell1 = textTable.cellAt(row + 1, 1) cell2 = textTable.cellAt(row + 1, 2) if (row + 1) % 2 == 0: cell0.setFormat(alternate_background) cell1.setFormat(alternate_background) cell2.setFormat(alternate_background) cellCursor0 = cell0.firstCursorPosition() cellCursor0.insertText(str(key)) cellCursor1 = cell1.firstCursorPosition() cellCursor1.insertText(str(data[key][0])) cellCursor2 = cell2.firstCursorPosition() cellCursor2.insertText(str(data[key][1])) row += 1 cursor.movePosition(QTextCursor.End, QTextCursor.MoveAnchor) cursor.insertHtml(""" <br><p style='text-align: center;'><strong>Current Frame</strong></p><br> """) centerFormat = QTextBlockFormat() centerFormat.setAlignment(Qt.AlignHCenter) cursor.insertBlock(centerFormat) cursor.insertImage(frame.scaledToWidth(500)) QCoreApplication.processEvents() document.print_(printer) QCoreApplication.processEvents()
def SaveAsPDF(self): """ Save Table as pdf """ fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "PDF Files (*.pdf)") if fileName == "": return try: videoName = self.player.fileName timestamp = self.player.player.position() QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) rows = self.VManager.rowCount() columns = self.VManager.columnCount() printer = QPrinter(QPrinter.HighResolution) innerRect = printer.pageRect() sizeF = QSizeF(innerRect.size().width(), innerRect.size().height()) header = QTextDocument() header.setPageSize(sizeF) cursor_header = QTextCursor(header) format1 = QTextCharFormat() format1.setFontPointSize(16) cursor_header.insertHtml( "<p style='text-align: left;'><strong>Video</strong>: %s <strong> TimeStamp</strong>: %s </p>" % (videoName, timestamp)) cursor_header.insertHtml("<br><br><br> ") cursor_header.select(QTextCursor.Document) fragment_header = cursor_header.selection() document = QTextDocument() cursor = QTextCursor(document) tableFormat = QTextTableFormat() tableFormat.setHeaderRowCount(1) tableFormat.setBorderBrush(QBrush(Qt.black)) tableFormat.setAlignment(Qt.AlignHCenter) tableFormat.setCellPadding(5) tableFormat.setCellSpacing(5) cursor.movePosition(QTextCursor.Start, QTextCursor.MoveAnchor) cursor.insertFragment(fragment_header) textTable = cursor.insertTable(rows + 1, columns, tableFormat) tableHeaderFormat = QTextCharFormat() tableHeaderFormat.setBackground(QColor("#DADADA")) for column in range(columns): cell = textTable.cellAt(0, column) cell.setFormat(tableHeaderFormat) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.horizontalHeaderItem(column).data( Qt.DisplayRole)) for row in range(rows): for column in range(columns): item = self.VManager.item(row, column) if item is not None: cell = textTable.cellAt(row + 1, column) cellCursor = cell.firstCursorPosition() cellCursor.insertText( self.VManager.item(row, column).text()) cursor.movePosition(QTextCursor.End) printer.setOrientation(QPrinter.Portrait) printer.setPageMargins(30, 100, 10, 40, QPrinter.DevicePixel) printer.setFullPage(True) printer.setPageSize(QPrinter.A4) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(fileName) document.print_(printer) QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage( QCoreApplication.translate("QgsFmvMetadata", "Succesfully creating PDF")) except Exception as e: QApplication.restoreOverrideCursor() qgsu.showUserAndLogMessage(QCoreApplication.translate( "QgsFmvMetadata", "Failed creating PDF"), level=QGis.Warning) return return