SHARE
TWEET

Untitled

a guest Feb 26th, 2020 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def printDialog(self):
  2.         filePath, filter = QFileDialog.getOpenFileName(self, 'Open file', '', 'Text (*.txt);;PDF (*.pdf)')
  3.         if not filePath:
  4.           return
  5.         file_extension = os.path.splitext(filePath)[1]
  6.         if file_extension == ".txt":
  7.             doc = QtGui.QTextDocument()
  8.             try:
  9.                 with open(filePath, 'r') as txtFile:
  10.                     doc.setPlainText(txtFile.read())
  11.                 printer = QPrinter(QPrinter.HighResolution)
  12.                 if not QPrintDialog(printer, self).exec_():
  13.                     return
  14.                 doc.print_(printer)
  15.             except Exception as e:
  16.                 print('Error trying to print: {}'.format(e))
  17.         elif file_extension == ".pdf":
  18.             #print ("PDF file")
  19.             #os.startfile("doc.pdf", "print")
  20.             #printer = QPrintDialog(QPrinter.HighResolution)
  21.             #printer.setOutputFormat(QPrinter.PdfFormat)
  22.             #printer.setOutputFileName(filePath)
  23.             current_dir = os.path.dirname(os.path.abspath(__file__))
  24.             self._page = QtWebEngineWidgets.QWebEnginePage()
  25.             #self._page.setHtml('''<img src="header.jpeg" alt="logo" width="280" height="100">''', QtCore.QUrl.fromLocalFile(os.path.join(current_dir, "index.html")))
  26.             self._page.setUrl(QtCore.QUrl.fromLocalFile(filePath))
  27.             self._printer = QtPrintSupport.QPrinter()
  28.             self._printer.setPaperSize(QtCore.QSizeF(80 ,297), QtPrintSupport.QPrinter.Millimeter)
  29.             r = QtPrintSupport.QPrintDialog(self._printer)
  30.             if r.exec_() == QtPrintSupport.QPrintDialog.Accepted:
  31.                 self._page.print(self._printer, self.print_completed)
  32.         else:
  33.             pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top