検索
ホームページバックエンド開発Python チュートリアルpython3+PyQt5でドキュメント印刷機能を実装

python3+PyQt5でドキュメント印刷機能を実装

Apr 24, 2018 am 11:40 AM
関数印刷する

この記事では主に python3+PyQt5 のドキュメント印刷機能を詳しく紹介します。興味のある方は参考にしてください。

この記事は Python3+PyQt5 を使用して「Python Qt Gui Fast」プログラミングを実装します。本書第 13 章の文書印刷機能。この記事では 3 つの方法を使用します:

1. HTML と QTextDOcument を使用してドキュメントを印刷します
2. QTextCusor と QTextDocument を使用してドキュメントを印刷します

Qpainter を使用してドキュメントを印刷するには、より複雑な計算が必要ですQTextDocument よりも優れていますが、QPainter は出力を完全に制御します。

#!/usr/bin/env python3
import math
import sys
import html
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog
from PyQt5.QtCore import (QDate, QRectF, Qt)
from PyQt5.QtWidgets import (QApplication,QDialog, 
 QHBoxLayout,QPushButton, QTableWidget, QTableWidgetItem,QVBoxLayout)
from PyQt5.QtGui import (QFont,QFontMetrics,QPainter,QTextCharFormat,
    QTextCursor, QTextDocument, QTextFormat,
    QTextOption, QTextTableFormat,
    QPixmap,QTextBlockFormat)
import qrc_resources


from PyQt5.QtPrintSupport import QPrinter,QPrintDialog
from PyQt5.QtCore import (QDate, QRectF, Qt)
from PyQt5.QtWidgets import (QApplication,QDialog, 
 QHBoxLayout,QPushButton, QTableWidget, QTableWidgetItem,QVBoxLayout)
from PyQt5.QtGui import (QFont,QFontMetrics,QPainter,QTextCharFormat,
    QTextCursor, QTextDocument, QTextFormat,
    QTextOption, QTextTableFormat,
    QPixmap,QTextBlockFormat)
import qrc_resources
DATE_FORMAT = "MMM d, yyyy"


class Statement(object):

 def __init__(self, company, contact, address):
 self.company = company
 self.contact = contact
 self.address = address
 self.transactions = [] # List of (QDate, float) two-tuples


 def balance(self):
 return sum([amount for date, amount in self.transactions])


class Form(QDialog):

 def __init__(self, parent=None):
 super(Form, self).__init__(parent)

 self.printer = QPrinter()
 self.printer.setPageSize(QPrinter.Letter)
 self.generateFakeStatements()
 self.table = QTableWidget()
 self.populateTable()

 cursorButton = QPushButton("Print via Q&Cursor")
 htmlButton = QPushButton("Print via &HTML")
 painterButton = QPushButton("Print via Q&Painter")
 quitButton = QPushButton("&Quit")

 buttonLayout = QHBoxLayout()
 buttonLayout.addWidget(cursorButton)
 buttonLayout.addWidget(htmlButton)
 buttonLayout.addWidget(painterButton)
 buttonLayout.addStretch()
 buttonLayout.addWidget(quitButton)
 layout = QVBoxLayout()
 layout.addWidget(self.table)
 layout.addLayout(buttonLayout)
 self.setLayout(layout)

 cursorButton.clicked.connect(self.printViaQCursor)
 htmlButton.clicked.connect(self.printViaHtml)
 painterButton.clicked.connect(self.printViaQPainter)
 quitButton.clicked.connect(self.accept)

 self.setWindowTitle("Printing")


 def generateFakeStatements(self):
 self.statements = []
 statement = Statement("Consality", "Ms S. Royal",
  "234 Rue Saint Hyacinthe, 750201, Paris")
 statement.transactions.append((QDate(2007, 8, 11), 2342))
 statement.transactions.append((QDate(2007, 9, 10), 2342))
 statement.transactions.append((QDate(2007, 10, 9), 2352))
 statement.transactions.append((QDate(2007, 10, 17), -1500))
 statement.transactions.append((QDate(2007, 11, 12), 2352))
 statement.transactions.append((QDate(2007, 12, 10), 2352))
 statement.transactions.append((QDate(2007, 12, 20), -7500))
 statement.transactions.append((QDate(2007, 12, 20), 250))
 statement.transactions.append((QDate(2008, 1, 10), 2362))
 self.statements.append(statement)

 statement = Statement("Demamitur Plc", "Mr G. Brown",
  "14 Tall Towers, Tower Hamlets, London, WC1 3BX")
 statement.transactions.append((QDate(2007, 5, 21), 871))
 statement.transactions.append((QDate(2007, 6, 20), 542))
 statement.transactions.append((QDate(2007, 7, 20), 1123))
 statement.transactions.append((QDate(2007, 7, 20), -1928))
 statement.transactions.append((QDate(2007, 8, 13), -214))
 statement.transactions.append((QDate(2007, 9, 15), -3924))
 statement.transactions.append((QDate(2007, 9, 15), 2712))
 statement.transactions.append((QDate(2007, 9, 15), -273))
 #statement.transactions.append((QDate(2007, 11, 8), -728))
 #statement.transactions.append((QDate(2008, 2, 7), 228))
 #statement.transactions.append((QDate(2008, 3, 13), -508))
 #statement.transactions.append((QDate(2008, 3, 22), -2481))
 #statement.transactions.append((QDate(2008, 4, 5), 195))
 self.statements.append(statement)


 def populateTable(self):
 headers = ["Company", "Contact", "Address", "Balance"]
 self.table.setColumnCount(len(headers))
 self.table.setHorizontalHeaderLabels(headers)
 self.table.setRowCount(len(self.statements))
 for row, statement in enumerate(self.statements):
  self.table.setItem(row, 0, QTableWidgetItem(statement.company))
  self.table.setItem(row, 1, QTableWidgetItem(statement.contact))
  self.table.setItem(row, 2, QTableWidgetItem(statement.address))
  item = QTableWidgetItem("$ {0:,.2f}".format(float(statement.balance())))
  item.setTextAlignment(Qt.AlignRight|Qt.AlignVCenter)
  self.table.setItem(row, 3, item)
 self.table.resizeColumnsToContents()


 def printViaHtml(self):
 htmltext = ""
 for statement in self.statements:
  date = QDate.currentDate().toString(DATE_FORMAT)
  address = html.escape(statement.address).replace(
   ",", "<br>")
  contact = html.escape(statement.contact)
  balance = statement.balance()
  htmltext += ("<p align=right><img  src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/153/291/f0f03360398e47554bc4a4fab2cf2da7-0.png?x-oss-process=image/resize,p_40"  class="lazy"  src=&#39;:/logo.png&#39; alt="python3+PyQt5でドキュメント印刷機能を実装" ></p>"
   "<p align=right>Greasy Hands Ltd."
   "<br>New Lombard Street"
   "<br>London<br>WC13 4PX<br>{0}</p>"
   "<p>{1}</p><p>Dear {2},</p>"
   "<p>The balance of your account is $ {3:,.2f}.").format(
   date, address, contact, float(balance))
  if balance < 0:
  htmltext += (" <p><font color=red><b>Please remit the "
    "amount owing immediately.</b></font>")
  else:
  htmltext += (" We are delighted to have done business "
    "with you.")
  htmltext += ("</p><p> </p><p>"
   "<table border=1 cellpadding=2 "
   "cellspacing=2><tr><td colspan=3>"
   "Transactions</td></tr>")
  for date, amount in statement.transactions:
  color, status = "black", "Credit"
  if amount < 0:
   color, status = "red", "Debit"
  htmltext += ("<tr><td align=right>{0}</td>"
    "<td>{1}</td><td align=right>"
    "<font color={2}>$ {3:,.2f}</font></td></tr>".format(
    date.toString(DATE_FORMAT), status, color,float(abs(amount))))
  htmltext += ("</table></p><p style=&#39;page-break-after:always;&#39;>"
   "We hope to continue doing "
   "business with you,<br>Yours sincerely,"
   "<br><br>K. Longrey, Manager</p>")
 dialog = QPrintDialog(self.printer, self)
 if dialog.exec_():
  document = QTextDocument()
  document.setHtml(htmltext)
  document.print_(self.printer)

 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 printViaQPainter(self):
 dialog = QPrintDialog(self.printer, self)
 if not dialog.exec_():
  return
 LeftMargin = 72
 sansFont = QFont("Helvetica", 10)
 sansLineHeight = QFontMetrics(sansFont).height()
 serifFont = QFont("Times", 11)
 fm = QFontMetrics(serifFont)
 DateWidth = fm.width(" September 99, 2999 ")
 CreditWidth = fm.width(" Credit ")
 AmountWidth = fm.width(" W999999.99 ")
 serifLineHeight = fm.height()
 logo = QPixmap(":/logo.png")
 painter = QPainter(self.printer)
 pageRect = self.printer.pageRect()
 page = 1
 for statement in self.statements:
  painter.save()
  y = 0
  x = pageRect.width() - logo.width() - LeftMargin
  painter.drawPixmap(x, 0, logo)
  y += logo.height() + sansLineHeight
  painter.setFont(sansFont)
  painter.drawText(x, y, "Greasy Hands Ltd.")
  y += sansLineHeight
  painter.drawText(x, y, "New Lombard Street")
  y += sansLineHeight
  painter.drawText(x, y, "London")
  y += sansLineHeight
  painter.drawText(x, y, "WC13 4PX")
  y += sansLineHeight
  painter.drawText(x, y,
   QDate.currentDate().toString(DATE_FORMAT))
  y += sansLineHeight
  painter.setFont(serifFont)
  x = LeftMargin
  for line in statement.address.split(", "):
  painter.drawText(x, y, line)
  y += serifLineHeight
  y += serifLineHeight
  painter.drawText(x, y, "Dear {0},".format(statement.contact))
  y += serifLineHeight

  balance = statement.balance()
  painter.drawText(x, y, "The balance of your account is $ {0:,.2f}".format(float(balance)))
  y += serifLineHeight
  if balance < 0:
  painter.setPen(Qt.red)
  text = "Please remit the amount owing immediately."
  else:
  text = ("We are delighted to have done business "
   "with you.")
  painter.drawText(x, y, text)
  painter.setPen(Qt.black)
  y += int(serifLineHeight * 1.5)
  painter.drawText(x, y, "Transactions:")
  y += serifLineHeight

  option = QTextOption(Qt.AlignRight|Qt.AlignVCenter)
  for date, amount in statement.transactions:
  x = LeftMargin
  h = int(fm.height() * 1.3)
  painter.drawRect(x, y, DateWidth, h)
  painter.drawText(
   QRectF(x + 3, y + 3, DateWidth - 6, h - 6),
   date.toString(DATE_FORMAT), option)
  x += DateWidth
  painter.drawRect(x, y, CreditWidth, h)
  text = "Credit"
  if amount < 0:
   text = "Debit"
  painter.drawText(
   QRectF(x + 3, y + 3, CreditWidth - 6, h - 6),
   text, option)
  x += CreditWidth
  painter.drawRect(x, y, AmountWidth, h)
  if amount < 0:
   painter.setPen(Qt.red)
  painter.drawText(
   QRectF(x + 3, y + 3, AmountWidth - 6, h - 6),
   "$ {0:,.2f}".format(float(amount)),
   option)
  painter.setPen(Qt.black)
  y += h
  y += serifLineHeight
  x = LeftMargin
  painter.drawText(x, y, "We hope to continue doing "
     "business with you,")
  y += serifLineHeight
  painter.drawText(x, y, "Yours sincerely")
  y += serifLineHeight * 3
  painter.drawText(x, y, "K. Longrey, Manager")
  x = LeftMargin
  y = pageRect.height() - 72
  painter.drawLine(x, y, pageRect.width() - LeftMargin, y)
  y += 2
  font = QFont("Helvetica", 9)
  font.setItalic(True)
  painter.setFont(font)
  option = QTextOption(Qt.AlignCenter)
  option.setWrapMode(QTextOption.WordWrap)
  painter.drawText(
   QRectF(x, y, pageRect.width() - 2 * LeftMargin, 31),
   "The contents of this letter are for information "
   "only and do not form part of any contract.",
   option)
  page += 1
  if page <= len(self.statements):
  self.printer.newPage()
  painter.restore()


if __name__ == "__main__":
 app = QApplication(sys.argv)
 form = Form()
 form.show()
 app.exec_()

実行結果:

関連する推奨事項:


python3+PyQt5 は、マルチスレッドをサポートするページ インデクサー アプリケーションを実装します

python3+PyQt5 汎用デリゲートによる詳しい説明

以上がpython3+PyQt5でドキュメント印刷機能を実装の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
Python vs. C:曲線と使いやすさの学習Python vs. C:曲線と使いやすさの学習Apr 19, 2025 am 12:20 AM

Pythonは学習と使用が簡単ですが、Cはより強力ですが複雑です。 1。Python構文は簡潔で初心者に適しています。動的なタイピングと自動メモリ管理により、使いやすくなりますが、ランタイムエラーを引き起こす可能性があります。 2.Cは、高性能アプリケーションに適した低レベルの制御と高度な機能を提供しますが、学習しきい値が高く、手動メモリとタイプの安全管理が必要です。

Python vs. C:メモリ管理とコントロールPython vs. C:メモリ管理とコントロールApr 19, 2025 am 12:17 AM

PythonとCは、メモリ管理と制御に大きな違いがあります。 1。Pythonは、参照カウントとガベージコレクションに基づいて自動メモリ管理を使用し、プログラマーの作業を簡素化します。 2.Cには、メモリの手動管理が必要であり、より多くの制御を提供しますが、複雑さとエラーのリスクが増加します。どの言語を選択するかは、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

科学コンピューティングのためのPython:詳細な外観科学コンピューティングのためのPython:詳細な外観Apr 19, 2025 am 12:15 AM

科学コンピューティングにおけるPythonのアプリケーションには、データ分析、機械学習、数値シミュレーション、視覚化が含まれます。 1.numpyは、効率的な多次元配列と数学的関数を提供します。 2。ScipyはNumpy機能を拡張し、最適化と線形代数ツールを提供します。 3. Pandasは、データ処理と分析に使用されます。 4.matplotlibは、さまざまなグラフと視覚的な結果を生成するために使用されます。

PythonとC:適切なツールを見つけるPythonとC:適切なツールを見つけるApr 19, 2025 am 12:04 AM

PythonまたはCを選択するかどうかは、プロジェクトの要件に依存するかどうかは次のとおりです。1)Pythonは、簡潔な構文とリッチライブラリのため、迅速な発展、データサイエンス、スクリプトに適しています。 2)Cは、コンピレーションと手動メモリ管理のため、システムプログラミングやゲーム開発など、高性能および基礎となる制御を必要とするシナリオに適しています。

データサイエンスと機械学習のためのPythonデータサイエンスと機械学習のためのPythonApr 19, 2025 am 12:02 AM

Pythonは、データサイエンスと機械学習で広く使用されており、主にそのシンプルさと強力なライブラリエコシステムに依存しています。 1)Pandasはデータ処理と分析に使用され、2)Numpyが効率的な数値計算を提供し、3)SCIKIT-LEARNは機械学習モデルの構築と最適化に使用されます。これらのライブラリは、Pythonをデータサイエンスと機械学習に理想的なツールにします。

Pythonの学習:2時間の毎日の研究で十分ですか?Pythonの学習:2時間の毎日の研究で十分ですか?Apr 18, 2025 am 12:22 AM

Pythonを1日2時間学ぶだけで十分ですか?それはあなたの目標と学習方法に依存します。 1)明確な学習計画を策定し、2)適切な学習リソースと方法を選択します。3)実践的な実践とレビューとレビューと統合を練習および統合し、統合すると、この期間中にPythonの基本的な知識と高度な機能を徐々に習得できます。

Web開発用のPython:主要なアプリケーションWeb開発用のPython:主要なアプリケーションApr 18, 2025 am 12:20 AM

Web開発におけるPythonの主要なアプリケーションには、DjangoおよびFlaskフレームワークの使用、API開発、データ分析と視覚化、機械学習とAI、およびパフォーマンスの最適化が含まれます。 1。DjangoandFlask Framework:Djangoは、複雑な用途の迅速な発展に適しており、Flaskは小規模または高度にカスタマイズされたプロジェクトに適しています。 2。API開発:フラスコまたはdjangorestFrameworkを使用して、Restfulapiを構築します。 3。データ分析と視覚化:Pythonを使用してデータを処理し、Webインターフェイスを介して表示します。 4。機械学習とAI:Pythonは、インテリジェントWebアプリケーションを構築するために使用されます。 5。パフォーマンスの最適化:非同期プログラミング、キャッシュ、コードを通じて最適化

Python vs. C:パフォーマンスと効率の探索Python vs. C:パフォーマンスと効率の探索Apr 18, 2025 am 12:20 AM

Pythonは開発効率でCよりも優れていますが、Cは実行パフォーマンスが高くなっています。 1。Pythonの簡潔な構文とリッチライブラリは、開発効率を向上させます。 2.Cのコンピレーションタイプの特性とハードウェア制御により、実行パフォーマンスが向上します。選択を行うときは、プロジェクトのニーズに基づいて開発速度と実行効率を比較検討する必要があります。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境