【问题标题】:Converting multiple HTML files to PDF using PyQt5使用 PyQt5 将多个 HTML 文件转换为 PDF
【发布时间】:2020-08-12 20:23:28
【问题描述】:

我尝试遵循这个答案: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:", 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")

【问题讨论】:

  • 你应该load() url的部分在哪里?
  • @musicamante 加载网址是什么意思?我正在尝试使用它们的路径转换存储在列表 htmls 中的本地文件
  • 即使您使用的是本地文件,这些文件路径也必须加载(使用QUrl)。在您的代码中,您只需分配self._current_path 的值,然后您什么都不做。那么网页应该如何加载呢?请,一旦您在答案中获得了一些代码,请尽力了解 它的作用 并研究与之相关的文档!此外,像这样使用zip() 毫无意义。

标签: python python-3.x pyqt pyqt5


【解决方案1】:

看来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")

【讨论】:

  • 我尝试实现您的代码,但程序仍然只转换列表中的第一个 html 文件,而不是全部。
  • @Ray234 糟糕,再试一次。
  • 这适用于两个 html 文件的列表,但不是更多?
  • @Ray234 你确定吗?我已经测试了 3 个文件并且它可以工作,你能指出你在控制台中得到的那些吗?
  • 所以生成的html列表是:[Q:\Ray\test1, Q:\Ray\test2, Q:\Ray\index1, Q:\Ray\index2] 但只有前两个从列表中获得转换
猜你喜欢
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2011-06-17
  • 2012-05-05
  • 2012-02-22
相关资源
最近更新 更多