PyQt:向打印机发送多个字符串

时间:2012-07-14 10:40:33

标签: python sqlite printing pyqt

我的应用程序很快就会出现,我要求帮助的所有问题最好是:)我想做的最后一件事就是能够打印(或报告)从数据库查询的各种字符串。我google了很多,找到了ReportLab Toolkit,reStructuredText等等,但这不是我想要的。

这是我的代码:(由于粘贴,缩进可能略有偏差)

def preparePrint(self):
 caseNumber = (str(self.ui.linecaseNumber.text()).upper())
 if caseNumber:
  with con:
   cur = con.cursor()
   cur.execute('SELECT notes FROM cases WHERE caseNumber =?',[caseNumber])
   tempnotes=str(cur.fetchone())
   cur.execute('SELECT name FROM cases WHERE caseNumber =?',[caseNumber])
   tempname=str(cur.fetchone())
   cur.execute('SELECT date FROM cases WHERE caseNumber =?',[caseNumber])
   tempdate = str(cur.fetchone()) #fetch 3 values from SQLite database

    #get rid of "tuple marks"
   tempname = (str(tempname.replace('(','').replace(')','').replace(',','').replace("'","")))
   tempnotes = (str(tempnotes.replace('(','').replace(')','').replace(',','').replace("'",""))) 
   tempdate=(str(tempdate.replace('(','').replace(')','').replace(',','').replace("'",""))) 

    #join them in one string. Notice the \n
   finalText = (str("NAME: %s\nDATE: %s\nNOTES: %s" %(tempname,tempdate,tempnotes))) 

#put the joined string into one QTextEdit, while preserving HTML properties (bold mostly).
   self.ui.textPrint.setHtml(str(finalText.replace('(','').replace(')','').replace(',','').replace("'","").replace('\\n','\r')))  

在此功能之后,有一个用于创建除了换行符之外的PDF。当我将 finalText 打印到控制台时,它看起来就像我想要的那样。它在QTextEdit中看起来不像那样:(

控制台输出:

Name: Some Name
Date: 14.07.2012
Notes: Some notes go here

QTextEdit和PDF输出:

Name: Some Name Date: 14.07.2012 Notes: Some notes go here

2 个答案:

答案 0 :(得分:1)

这个代码在很多地方都错了......
我通过许多改进和适当的行长度修改了代码:

def preparePrint(self):
    caseNumber = str(self.ui.linecaseNumber.text()).upper()
    if caseNumber:
        with con:
            cur = con.cursor()
            # fetch 3 values from SQLite database
            cur.execute('SELECT notes FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempnotes = cur.fetchone()[0]
            # if you have multiple columns, use:
            # tempnotes = ''.join(cur.fetchone())
            cur.execute('SELECT name FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempname = cur.fetchone()[0]
            cur.execute('SELECT date FROM cases WHERE caseNumber = ?',
                        caseNumber)
            tempdate = cur.fetchone()[0]

        # no need to 'get rid of "tuple marks"',
        # because they aren't there now

        # join them in one string. Notice the \n
        finalText = ("NAME: %s\nDATE: %s\nNOTES: %s"
                     % (tempname, tempdate, tempnotes))

        # put the joined string into one QTextEdit, 
        # while preserving HTML properties (bold mostly).

        # again, no need to 'get rid of "tuple marks"'
        self.ui.textPrint.setHtml(finaltext)

这应该使文本换行。如果没有,请尝试在finaltext中使用\r\n而不是\n。如果它仍然不起作用,那么奇怪的事情就会发生,但至少你有更好的代码。

答案 1 :(得分:0)

我破解了! :d 由于QTextEdit接受HTML,< br>标签将生成换行符。这是一段与原始帖子不同的代码:

finalText = (str("NAME: %s%sDATE: %s%sNOTES: %s%s" %(tempname,'<br>',tempdate,'<br>',tempnotes,'<br>')))
self.ui.textPrint.setHtml(str(finalText).replace('\\n','\r'))