Frage

How can I print my data from my database (sqlite) into a notepad /word document from my table in my GUI (using the same formatting). Here is my code for the table which is represented on my gui.

class Table(QtGui.QDialog):
    def __init__(self):
        super(Table, self).__init__()
        with sqlite3.connect('database.db') as db:
            cursor=db.cursor()
            cursor.execute('select* from Receipt Order BY ReceiptID ASC')
            title = [cn[0] for cn in cursor.description]
            rows = [cn[0] for cn in cursor.description]
            cur=cursor.fetchall()
            layout = QtGui.QGridLayout() 
            self.table = QtGui.QTableWidget()
            self.setGeometry(500,500,500,400)
            qr = self.frameGeometry()
            cp = QtGui.QDesktopWidget().availableGeometry().center()
            qr.moveCenter(cp)
            self.move(qr.topLeft())
            self.label2=QtGui.QLabel(self)
            self.label2.setPixmap(QtGui.QPixmap('receipt_pic.jpg'))
            self.label2.setGeometry(0,0,500,400)
            self.table.setColumnCount(2)
            self.table.setHorizontalHeaderLabels(title)
            for i,row in enumerate(cur):
                self.table.insertRow(self.table.rowCount())
                for j,val in enumerate(row):
                    self.table.setItem(i, j, QtGui.QTableWidgetItem(str(val)))
            layout.addWidget(self.table, 0, 0)
            self.setLayout(layout)
            self.setWindowTitle('Receipt Table')

I want to be able to click a button which copies this information (which would appear as a table with filled columns and rows) into a seperate notepad file / or any text document (where I can send the table to the printer to be printed).

War es hilfreich?

Lösung

It would probably be easier to just print the table directly, rather than use an intermediary file.

To do this, you can use a QTextDocument to create a printable representation of the table, and then use the built-in print dialogs to do the rest.

So, first add some buttons:

    ...
    layout.addWidget(self.table, 0, 0, 1, 2)
    self.buttonPrint = QtGui.QPushButton('Print', self)
    self.buttonPrint.clicked.connect(self.handlePrint)
    self.buttonPreview = QtGui.QPushButton('Preview', self)
    self.buttonPreview.clicked.connect(self.handlePreview)
    layout.addWidget(self.buttonPrint, 1, 0)
    layout.addWidget(self.buttonPreview, 1, 1)
    self.setLayout(layout)
    ...

then some handlers for the print and print-preview dialogs:

def handlePrint(self):
    dialog = QtGui.QPrintDialog()
    if dialog.exec_() == QtGui.QDialog.Accepted:
        self.handlePaintRequest(dialog.printer())

def handlePreview(self):
    dialog = QtGui.QPrintPreviewDialog()
    dialog.paintRequested.connect(self.handlePaintRequest)
    dialog.exec_()

and finally some methods for creating the document and printing it:

def handlePaintRequest(self, printer):
    document = self.makeTableDocument()
    document.print_(printer)

def makeTableDocument(self):
    document = QtGui.QTextDocument()
    cursor = QtGui.QTextCursor(document)
    rows = self.table.rowCount()
    columns = self.table.columnCount()
    table = cursor.insertTable(rows + 1, columns)
    format = table.format()
    format.setHeaderRowCount(1)
    table.setFormat(format)
    format = cursor.blockCharFormat()
    format.setFontWeight(QtGui.QFont.Bold)
    for column in range(columns):
        cursor.setCharFormat(format)
        cursor.insertText(
            self.table.horizontalHeaderItem(column).text())
        cursor.movePosition(QtGui.QTextCursor.NextCell)
    for row in range(rows):
        for column in range(columns):
            cursor.insertText(
                self.table.item(row, column).text())
            cursor.movePosition(QtGui.QTextCursor.NextCell)
    return document

If you prefer to save to a file, you can use QTextDocument.toHtml to dump the table as html.

The printed results are quite basic, but if you want something more fancy, you can always build the document using html/css.

Andere Tipps

I use this function

    def LoadDatabase(self):
        self.banco = sqlite3.connect ( 'Vendas.db' )
        self.cursor = banco.cursor ( )
        query = "SELECT * FROM Produtos"
        result = self.banco.execute ( query )
        self.listaprodutos.setRowCount ( 0 )
        for row_number, row_data in enumerate ( result ):
            self.listaprodutos.insertRow ( row_number )
            for colum_number, data in enumerate ( row_data ):
                self.listaprodutos.setItem ( row_number, colum_number, QtWidgets.QTableWidgetItem ( str ( data ) ) )
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top