首页 > 解决方案 > 使用 PyQt5 将多个 HTML 文件转换为 PDF

问题描述

我尝试遵循这个答案:如何使用 PyQT5 在一个循环中将多个 HTML 文档转换为 PDF

我对其进行了修改以转换在本地文件夹中找到的所有 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:", filePath, 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")

标签: pythonpython-3.xpyqtpyqt5

解决方案


似乎 OP 还没有理解我之前的解决方案的逻辑,即:

  1. 获取资源,在本例中为文件,
  2. 在页面上加载它,
  3. 加载完成后,打印页面内容,
  4. 打印完成后,使用下一个资源执行步骤 1。

在此它不执行步骤 2,另一方面,建议 pdf 的路径具有 html 以外的名称

import os
import glob
from PyQt5.QtCore import QUrl
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(htmls)
        self._fetchNext()

    def _fetchNext(self):
        try:
            self._current_path = next(self._htmls)
        except StopIteration:
            return False
        else:
            self.load(QUrl.fromLocalFile(self._current_path))
        return True

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

    def _handlePrintingFinished(self, filePath, success):
        print("finished:", filePath, 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)
    print(htmls)
    if htmls:
        app = QtWidgets.QApplication([])
        page = PdfPage()
        page.convert(htmls)
        app.exec_()
    print("finished")

推荐阅读