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 49 50 51 52 53
|
#Python 3.2
# -*- coding: utf-8 -*-
import sys, os
from PySide import QtCore, QtGui
#############################################################################
app = QtGui.QApplication(sys.argv)
# creer le document
doc = QtGui.QTextDocument()
cur = QtGui.QTextCursor(doc)
# creer le format de la table
tableFormat = QtGui.QTextTableFormat()
tableFormat.setAlignment(QtCore.Qt.AlignCenter) # la table sera au milieu.
tableFormat.setBorder(2.0)
tableFormat.setCellPadding(2)
tableFormat.setCellSpacing(0)
tableFormat.setBorderStyle(QtGui.QTextFrameFormat.BorderStyle_Solid)
tableFormat.setBorderBrush(QtGui.QColor(0,0,0))
# inserer la table 1x1 avec ce format
imax, jmax = 2, 1
table = cur.insertTable(imax, jmax, tableFormat)
### a imprimer sur la premiere page
# ecrire un texte dans la case
cellCur = table.cellAt(0, 0).firstCursorPosition()
cellCur.select(QtGui.QTextCursor.LineUnderCursor) # selectionner la ligne
cellCur.removeSelectedText() # effacer le texte anterieur
cellCur.insertText("Element de la première page")
### a imprimer sur la deuxieme page
cellCur = table.cellAt(1, 0).firstCursorPosition()
cellCur.select(QtGui.QTextCursor.LineUnderCursor) # selectionner la ligne
cellCur.removeSelectedText() # effacer le texte anterieur
cellCur.insertText("Element de la deuxième page")
# imprimer dans un pdf
fichierpdf = "sample.pdf"
printer = QtGui.QPrinter(QtGui.QPrinter.HighResolution)
printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
printer.setOutputFileName(fichierpdf)
doc.print_(printer)
# afficher le fichier pdf a* l'ecran en utilisant le pdfreader par defaut
url = QtCore.QUrl.fromLocalFile(os.path.abspath(fichierpdf))
QtGui.QDesktopServices.openUrl(url) |
Partager