通过循环将Qwebview打印为PDF

时间:2015-01-14 06:53:37

标签: python qt python-2.7 pyqt pyqt4

我尝试将其转换为html到pdf后尝试打印多个xml,下面是我的代码,当我尝试运行它时只将PDF文件打印到输出文件夹但只有最后一个文件获取其他文件的完整内容只有一页空白,我不知道哪里出错了。

 def process(self, MainWindow):
        inputpath = str(self.lineEdit.text())
        outputfile = str(self.lineEdit_2.text())

        tree = ET1.parse('config.xml')
        root = tree.getroot()
        xsltfile = root.findtext('xsltpath')
        for dirpath, dirnames, filenames in os.walk(inputpath):
            for filename in filenames:
                if filename.endswith('.xml'):
                    inputfile = os.path.join(dirpath, filename)
                    dom = ET.parse(inputfile)
                    xslt = ET.parse(xsltfile)
                    transform = ET.XSLT(xslt)
                    newdom = transform(dom)
                    infile = (ET.tostring(newdom, pretty_print=True))
                    web = QWebView()
                    html = infile
                    web.setHtml( html )
                    filename2 = filename.rsplit(".", 1) [0]
                    outfil = outputfile + "\\" + filename2 + ".pdf"
                    printer = QPrinter()
                    printer.setPageSize(QPrinter.A4)
                    printer.setOutputFormat(QPrinter.PdfFormat)
                    printer.setOutputFileName(outfil)

                    def convertIt():
                        web.print_(printer)
                    QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)
                    #web.show()
                    print outfil
                    time.sleep(20)                    
                    print ("processed")
        QtGui.QMessageBox.about(self, 'Completed', 'Process Completed')

被修改 当我把msg box" QtGui.QMessageBox.about(self,' Completed',' Process Completed')"在循环中它给了我正确的输出,问题是因为它在webview中完全加载文件之前打印,我添加了" QObject.connect(web,SIGNAL(" loadFinished(bool)&#34 ;),convertIt)"要检查它是否已经完全加载,它似乎不起作用是否有更好的方法来检查它。

0 个答案:

没有答案