def handlePaintRequest(self, printer): printer.setDocName(self.fname) document = QTextDocument() cursor = QTextCursor(document) model = self.tableView.model() tableFormat = QTextTableFormat() tableFormat.setBorder(0.2) tableFormat.setBorderStyle(3) tableFormat.setCellSpacing(0) tableFormat.setTopMargin(0) tableFormat.setCellPadding(4) table = cursor.insertTable(model.rowCount() + 1, model.columnCount(), tableFormat) model = self.tableView.model() ### get headers myheaders = [] for i in range(0, model.columnCount()): myheader = model.headerData(i, Qt.Horizontal) cursor.insertText(str(myheader)) cursor.movePosition(QTextCursor.NextCell) ### get cells for row in range(0, model.rowCount()): for col in range(0, model.columnCount()): index = model.index(row, col) cursor.insertText(str(index.data())) cursor.movePosition(QTextCursor.NextCell) document.print_(printer)
def handlePaintRequest(self, printer): # find empty cells for row in range(self.tableView.rowCount()): for column in range(self.tableView.columnCount()): myitem = self.tableView.item(row, column) if myitem is None: item = QTableWidgetItem("") self.tableView.setItem(row, column, item) printer.setDocName(self.fname) document = QTextDocument() cursor = QTextCursor(document) model = self.tableView.model() tableFormat = QTextTableFormat() tableFormat.setBorder(0.2) tableFormat.setBorderStyle(3) tableFormat.setCellSpacing(0) tableFormat.setTopMargin(0) tableFormat.setCellPadding(4) table = cursor.insertTable(model.rowCount(), model.columnCount(), tableFormat) for row in range(table.rows()): for column in range(table.columns()): cursor.insertText(self.tableView.item(row, column).text()) cursor.movePosition(QTextCursor.NextCell) document.print_(printer)
def printPreview(self, obj, data): from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog from PyQt5.QtGui import QTextDocument, QTextCursor, QTextTableFormat, QTextFrameFormat #, QFont import time self.printer = QPrinter() self.printer.setPageSize(QPrinter.A4) self.printer.setOrientation(QPrinter.Landscape) self.printer.setFullPage(True) self.printer.setPageMargins(2, 2, 2, 2, QPrinter.Millimeter) #self.printer.setFont(QFont("times",22)) dialog = QPrintPreviewDialog(self.printer) #dialog.showFullScreen() document = QTextDocument() #document.setHtml("<html><head></head><body></body></html>") document.setHtml("") cursor = QTextCursor(document) # Titolo cursor.insertHtml("<h2 align=center>%s</h2>" % obj['title']) cursor.insertText("\n") # Data cursor.insertHtml("<h5 align=left>%s</h5>" % time.strftime("%d/%m/%Y")) cursor.insertText("\n") # Table tableFormat = QTextTableFormat() tableFormat.setCellPadding(2) tableFormat.setCellSpacing(3) #tableFormat.setBorderStyle(QTextFrameFormat.BorderStyle_Ridge) tableFormat.setBorder(0) cursor.insertTable(len(data) + 2, len(obj['col_name']), tableFormat) # Intestazioni for table_title in obj['col_name']: cursor.insertHtml('<font size="4" color="blue"><b>%s</b></font>' % table_title) cursor.movePosition(QTextCursor.NextCell) # Riga bianca for table_title in obj['col_name']: cursor.insertText(' ') cursor.movePosition(QTextCursor.NextCell) # Dati Tabella for r in data: for k in obj['col_order']: v = r[k] if v is not None: #cursor.insertText(str(v)) cursor.insertHtml('<font size="4">%s</font>' % v) cursor.movePosition(QTextCursor.NextCell) dialog.paintRequested.connect(document.print_) dialog.setFixedSize(1500, 1050) dialog.exec_()
def newLetter(self): self.textEdit.clear() cursor = self.textEdit.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) italicFormat = QTextCharFormat() italicFormat.setFontItalic(True) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(16) tableFormat.setAlignment(Qt.AlignRight) ##The Firm Box cursor.insertTable(1, 1, tableFormat) cursor.insertText("The Firm", boldFormat) cursor.insertBlock() cursor.insertText("321 City Street", textFormat) cursor.insertBlock() cursor.insertText("Industry Park") cursor.insertBlock() cursor.insertText("Some Country") cursor.setPosition(topFrame.lastPosition()) ##The Firm Box 아래 편지부분 cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) cursor.insertBlock() cursor.insertBlock() cursor.insertText("Dear ", textFormat) cursor.insertText("NAME", italicFormat) cursor.insertText(",", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("Yours sincerely,", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("The Boss", textFormat) cursor.insertBlock() cursor.insertText("ADDRESS", italicFormat)
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 newLetter(self): self.textEdit.clear() cursor = self.textEdit.textCursor() cursor.movePosition(QTextCursor.Start) topFrame = cursor.currentFrame() topFrameFormat = topFrame.frameFormat() topFrameFormat.setPadding(16) topFrame.setFrameFormat(topFrameFormat) textFormat = QTextCharFormat() boldFormat = QTextCharFormat() boldFormat.setFontWeight(QFont.Bold) italicFormat = QTextCharFormat() italicFormat.setFontItalic(True) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(16) tableFormat.setAlignment(Qt.AlignRight) cursor.insertTable(1, 1, tableFormat) cursor.insertText("The Firm", boldFormat) cursor.insertBlock() cursor.insertText("321 City Street", textFormat) cursor.insertBlock() cursor.insertText("Industry Park") cursor.insertBlock() cursor.insertText("Some Country") cursor.setPosition(topFrame.lastPosition()) cursor.insertText(QDate.currentDate().toString("d MMMM yyyy"), textFormat) cursor.insertBlock() cursor.insertBlock() cursor.insertText("Dear ", textFormat) cursor.insertText("NAME", italicFormat) cursor.insertText(",", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("Yours sincerely,", textFormat) for i in range(3): cursor.insertBlock() cursor.insertText("The Boss", textFormat) cursor.insertBlock() cursor.insertText("ADDRESS", italicFormat)
def printViaQCursor(self): dialog = QPrintDialog(self.printer, self) if not dialog.exec_(): return logo = QPixmap(":/logo.png") headFormat = QTextBlockFormat() headFormat.setAlignment(Qt.AlignLeft) headFormat.setTextIndent(self.printer.pageRect().width() - logo.width() - 216) bodyFormat = QTextBlockFormat() bodyFormat.setAlignment(Qt.AlignJustify) lastParaBodyFormat = QTextBlockFormat(bodyFormat) lastParaBodyFormat.setPageBreakPolicy( QTextFormat.PageBreak_AlwaysAfter) rightBodyFormat = QTextBlockFormat() rightBodyFormat.setAlignment(Qt.AlignRight) headCharFormat = QTextCharFormat() headCharFormat.setFont(QFont("Helvetica", 10)) bodyCharFormat = QTextCharFormat() bodyCharFormat.setFont(QFont("Times", 11)) redBodyCharFormat = QTextCharFormat(bodyCharFormat) redBodyCharFormat.setForeground(Qt.red) tableFormat = QTextTableFormat() tableFormat.setBorder(1) tableFormat.setCellPadding(2) document = QTextDocument() cursor = QTextCursor(document) mainFrame = cursor.currentFrame() page = 1 for statement in self.statements: cursor.insertBlock(headFormat, headCharFormat) cursor.insertImage(":/logo.png") for text in ("Greasy Hands Ltd.", "New Lombard Street", "London", "WC13 4PX", QDate.currentDate().toString(DATE_FORMAT)): cursor.insertBlock(headFormat, headCharFormat) cursor.insertText(text) for line in statement.address.split(", "): cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText(line) cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Dear {0},".format(statement.contact)) cursor.insertBlock(bodyFormat) cursor.insertBlock(bodyFormat, bodyCharFormat) balance = statement.balance() cursor.insertText( "The balance of your account is $ {0:,.2f}.".format( float(balance))) if balance < 0: cursor.insertBlock(bodyFormat, redBodyCharFormat) cursor.insertText("Please remit the amount owing " "immediately.") else: cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("We are delighted to have done " "business with you.") cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Transactions:") table = cursor.insertTable(len(statement.transactions), 3, tableFormat) row = 0 for date, amount in statement.transactions: cellCursor = table.cellAt(row, 0).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) cellCursor.insertText(date.toString(DATE_FORMAT), bodyCharFormat) cellCursor = table.cellAt(row, 1).firstCursorPosition() if amount > 0: cellCursor.insertText("Credit", bodyCharFormat) else: cellCursor.insertText("Debit", bodyCharFormat) cellCursor = table.cellAt(row, 2).firstCursorPosition() cellCursor.setBlockFormat(rightBodyFormat) format = bodyCharFormat if amount < 0: format = redBodyCharFormat cellCursor.insertText("$ {0:,.2f}".format(float(amount)), format) row += 1 cursor.setPosition(mainFrame.lastPosition()) cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("We hope to continue doing business " "with you,") cursor.insertBlock(bodyFormat, bodyCharFormat) cursor.insertText("Yours sincerely") cursor.insertBlock(bodyFormat) if page == len(self.statements): cursor.insertBlock(bodyFormat, bodyCharFormat) else: cursor.insertBlock(lastParaBodyFormat, bodyCharFormat) cursor.insertText("K. Longrey, Manager") page += 1 document.print_(self.printer)
def interp(self): self.reset() curve = self.curveCombobox.currentText().strip() if self.view is None: return if curve not in self.view.pointObjs or len( self.view.pointObjs[curve]) < 2: return xs = [] ys = [] for item in self.view.pointObjs[curve]: xs.append(item.pos().x()) ys.append(item.pos().y()) xs, ys = self.view.pointToCoord(xs, ys) xmin = None xmax = None xstep = None if self.minXTextBox.text().strip(): xmin = str2num(self.minXTextBox.text().strip()) self.view.proj.fitx[0] = xmin if xmin is None: xmin = min(xs) if self.maxXTextBox.text().strip(): xmax = str2num(self.maxXTextBox.text().strip()) self.view.proj.fitx[1] = xmax if xmax is None: xmax = max(xs) if self.stepXTextBox.text().strip(): xstep = str2num(self.stepXTextBox.text().strip()) self.view.proj.fitx[2] = xstep if xstep is None: xstep = (xmax - xmin) / 15 xnew = np.arange(xmin, xmax + xstep / 2, xstep) degree = int(self.degreeComboBox.currentText()) self.view.proj.degree = degree self.view.proj.precision = self.presicion ynew = interp(xs, ys, xnew, degree) xpos, ypos = self.view.coordToPoint(xnew, ynew) for i in range(1, len(xpos)): line = QGraphicsLineItem(xpos[i - 1], ypos[i - 1], xpos[i], ypos[i]) line.setZValue(10) line.setPen(QPen(Qt.yellow, 2, Qt.SolidLine)) self.interpCurveObjs.append(line) self.view.scene.addItem(line) self.minXTextBox.setText(str(xmin)) self.maxXTextBox.setText(str(xmax)) self.stepXTextBox.setText(str(xstep)) text = "interpolation points:\n\n" self.outTextBox.setText(text) cursor = QTextCursor(self.outTextBox.textCursor()) cursor.movePosition(QTextCursor.End) # move the end of documents ttf = QTextTableFormat() # 创建表格对象格式 ttf.setCellPadding(2) # 单元格内文本和边框距离 ttf.setCellSpacing(0) # 单元格线宽 ttf.setAlignment(Qt.AlignLeft) # 表格对齐模式 ttf.setBorder(0.5) # ttf.setColumnWidthConstraints( # (QTextLength(QTextLength.PercentageLength, 50), QTextLength(QTextLength.PercentageLength, 50))) # 百分比定义列宽 ttf.setColumnWidthConstraints( (QTextLength(QTextLength.FixedLength, 75), QTextLength(QTextLength.FixedLength, 75))) # 像素定义列宽 table = cursor.insertTable(len(xnew) + 1, 2, ttf) table.cellAt(0, 0).firstCursorPosition().insertHtml("<b>x</b>") table.cellAt(0, 1).firstCursorPosition().insertHtml("<b>y</b>") for i in range(len(xnew)): table.cellAt(i + 1, 0).firstCursorPosition().insertText( str(round(xnew[i], self.presicion))) table.cellAt(i + 1, 1).firstCursorPosition().insertText( str(round(ynew[i], self.presicion))) self.view.sigModified.emit(True)