Вопрос

I am trying to add a way to export data from an sqlite3 database into a pdf document. In the book rapid gui programming with python and Qt it says that I can export QTextDocuments as pdf files. However it need to be in the form of a QTextDocument. At the moment I have been trying to get a string of headers for the table into a pdf document as a test however when it is run idle just restarts.

    import sqlite3
    from PyQt5 import QtWidgets, QtCore, QtGui
    class Report(QtWidgets.QWidget):
        def __init__(self,text):
            super(Report,self).__init__()
            self.textArea = QTextEdit(self)
            self.cursor = QTextCursor(self.textArea.document())
            self.cursor.insertText(text)

    def createTextReport(fileName):
        headings = ('|%10s|'%'AssetID' + '|%40s|'%'Description' + '|%40s|'%'Room Description' + '|%5s|'%'Labelled' + '|%10s|'%'Condition')
        report = Report(headings)
        printer = QPrinter()
        printer.setPageSize(QPrinter.Letter)
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setOutputFileName('C:/Users/Luke/Documents/A-Level-Work/A2/Computing/COMP 4/Program/report.pdf')
        report.print_(printer)


    fileName = 'C:/Users/Luke/Documents/A-Level-Work/A2/Computing/COMP 4/Program/test_database.db'

    createTextReport(fileName)

I am wondering where i am going wrong and also is there a simple way of getting data from an SQL query into a QTextDocument format as a table so i don't have to format it manually. Please make explanations in simple terms as my programming knowledge is very limited.

Это было полезно?

Решение

If you just have tuples from your sqlite3 module, then you can format your data using something like this:

myText = ''

for entry in conn.execute('SOME SQL QUERY'):
    myText += '|{:10s}| AssetID |{:40s}| Description |{:40s}| Room Description |{:5s}| Labelled |{:10s}| Condition'.format(*entry)
    myText += '\n'

Then you can feed this text into your Report() object.

Used this post.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top