Python 如何阻止wkhtmltopdf.exe弹出?
我用python创建了一个GUI程序来创建PDF。我正在使用pdfkit库来创建PDF:Python 如何阻止wkhtmltopdf.exe弹出?,python,python-3.x,popup,wkhtmltopdf,pdfkit,Python,Python 3.x,Popup,Wkhtmltopdf,Pdfkit,我用python创建了一个GUI程序来创建PDF。我正在使用pdfkit库来创建PDF: options = { 'page-size': 'A4', 'margin-top': '0.3in', 'margin-bottom': '0.3in', 'margin-left': '0.5in', 'margin-right': '0.4in', 'quiet': '', 'orientation' : 'La
options = {
'page-size': 'A4',
'margin-top': '0.3in',
'margin-bottom': '0.3in',
'margin-left': '0.5in',
'margin-right': '0.4in',
'quiet': '',
'orientation' : 'Landscape'
}
toc = {
'xsl-style-sheet': 'toc.xsl'
}
path_wkhtmltopdf = r'wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
pdfkit.from_string(htmlString, reportPath, configuration=config, toc=toc, options = options)
为了制作GUI程序的可执行文件,我使用了pyinstaller
。
当我使用这个.exe文件时,它会在创建pdf时弹出一个wkhtmltopdf.exe的cmd窗口。我怎么才能不出现?在互联网上研究之后,我没有找到任何解决方案。虽然不是直接的,但弹出窗口来自模块
子进程调用的命令,该子进程默认为在调用wkhtmltopdf
的可执行文件时创建窗口
subprocess.CREATE_NEW_控制台
The new process has a new console, instead of inheriting its parent’s console (the default).
由于无法通过pdfkit
传递该参数,因此可以在从pyinstaller
生成之前找到要进行更改的模块。下面描述的方法在Windows和Python3.X上适用
找到并更改创建窗口的子流程
设置
import pdfkit
pdfkit.pdfkit
#THIS LOCATES THE FILE YOU NEED TO CHANGE
Output:
<module 'pdfkit.pdfkit' from '\\lib\\site-packages\\pdfkit\\pdfkit.py'>
并保存文件,这将传递禁止创建窗口的所需参数
一旦你完成了,你应该能够重建你的程序
示例程序
使用扩展名.pyw
保存以下代码,该扩展名基本上意味着来自Python的无控制台脚本,例如html2pdf.pyw
。确保将路径替换为您的路径
import pdfkit
path_wkhtmltopdf = 'your wkhtmltopdf.exe path'
out_path = 'the output file goes here'
config = pdfkit.configuration(wkhtmltopdf = path_wkhtmltopdf)
pdfkit.from_url('http://google.com',
out_path,
configuration = config)
找到html2pdf.pyw
文件夹并使用pyinstaller
构建:pyinstaller html2pdf
最后,使用位于同一文件夹下的可执行文件测试您的程序,该文件夹位于dist\html2pdf\html2pdf.exe
下,您就是救命稻草!我想说这个解决方案非常简单,易于实现。尽管如此,它也可以在不使用.pyw扩展名保存脚本的情况下工作。是的,.pyw
只是为了抑制Python控制台(如果有的话)。希望这有所帮助,在任何情况下,如果您认为有用,可以进一步建议一个选项,将args
传递到pdfkit
中,以便在不更改原始库代码的情况下传递参数。
import pdfkit
path_wkhtmltopdf = 'your wkhtmltopdf.exe path'
out_path = 'the output file goes here'
config = pdfkit.configuration(wkhtmltopdf = path_wkhtmltopdf)
pdfkit.from_url('http://google.com',
out_path,
configuration = config)