python - 使用 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")
解决方案
似乎 OP 还没有理解我之前的解决方案的逻辑,即:
- 获取资源,在本例中为文件,
- 在页面上加载它,
- 加载完成后,打印页面内容,
- 打印完成后,使用下一个资源执行步骤 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")
推荐阅读
- openldap - 尝试从客户端主机连接时出现以下错误
- angular - 角度错误:StaticInjectorError(AppModule)[GithubComponent -> String]
- javascript - 在 React 中通过数组索引 onClick 递增
- mysql - MySQL:Revenu 报告上升和下降,逐月
- python - Pyodbc 驱动程序为空
- python - 检查字段是否包含字符串(带标点符号)
- android - 如何在新的 ViewPager2 中禁用 Fragment 的销毁?
- java - Replace an image resource on a jasperserver via Java REST API v7.2.0
- ios - 在照片应用程序和文件和文件夹应用程序中显示 iOS Cordova 应用程序文件
- kubernetes-helm - 在 azure AKS 上忽略 nginx-ingress Fluxcd HelmRelease 中的 loadBalancerIP 和注释