【发布时间】: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