使用PyQt5将多个HTML文件转换为PDF

问题描述

我尝试遵循以下答案:How to use PyQT5 to convert multiple HTML docs to PDF in one loop

我对其进行了修改,以转换在本地文件夹中找到的所有html文件。例如htmls是要转换的html文件的列表:[Q:\ Ray \ test1.html,Q:\ Ray \ prac2.html]

这是代码。但是,当我尝试运行它时,Python会冻结,我必须停止运行。

import os
import glob
from PyQt5 import QtWidgets,QtWebEngineWidgets

class PdfPage(QtWebEngineWidgets.QWebEnginePage):
    def __init__(self):
        super().__init__()
        self._htmls = []
        self._current_path = ""

        self.setZoomFactor(1)
        self.loadFinished.connect(self._handleLoadFinished)
        self.pdfPrintingFinished.connect(self._handlePrintingFinished)

    def convert(self,htmls):
        self._htmls = iter(zip(htmls))
        self._fetchNext()

    def _fetchNext(self):
        try:
            self._current_path = next(self._htmls)
        except StopIteration:
            return False

    def _handleLoadFinished(self,ok):
        if ok:
            self.printToPdf(self._current_path)

    def _handlePrintingFinished(self,filePath,success):
        print("finished:",success)
        if not self._fetchNext():
            QtWidgets.QApplication.quit()


if __name__ == "__main__":

    current_dir = os.path.dirname(os.path.realpath(__file__))
    folder= current_dir+ '\\*.HTML'
    htmls= glob.glob(folder)

    app = QtWidgets.QApplication([])
    page = PdfPage()
    page.convert(htmls)
    app.exec_()

    print("finished")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)