0

sqlite3 データベースから pdf ドキュメントにデータをエクスポートする方法を追加しようとしています。PythonとQtを使用した迅速なGUIプログラミングの本の中で、QTextDocumentsをpdfファイルとしてエクスポートできると書かれています。ただし、QTextDocument の形式である必要があります。現時点では、テーブルのヘッダーの文字列をテストとしてpdfドキュメントに取得しようとしていますが、アイドル状態で実行すると再起動します。

    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)

どこが間違っているのか疑問に思っています。また、SQLクエリからQTextDocument形式にデータをテーブルとして取得する簡単な方法があるので、手動でフォーマットする必要はありません。私のプログラミング知識は非常に限られているので、簡単な言葉で説明してください。

4

1 に答える 1

1

モジュールからのタプルだけがある場合はsqlite3、次のようなものを使用してデータをフォーマットできます。

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'

Report()次に、このテキストをオブジェクトにフィードできます。

この投稿を使用しました。

于 2014-01-19T16:09:00.293 に答える