windows pdfkit [WinError 740]请求的操作需要提升python3

lkaoscv7  于 2023-01-06  发布在  Windows
关注(0)|答案(3)|浏览(475)

我想基于给定的URL将HTML页面转换为PDF文件。我尝试过pdfkit,但它抛出以下错误:
[WinError 740] The requested operation requires elevation.
代码:

import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )
pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)

输出错误:

n [42]: import pdfkit
path_wkthmltopdf = "D:\\w.exe"
config = pdfkit.configuration(wkhtmltopdf = path_wkthmltopdf )

pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)
Traceback (most recent call last):

  File "<ipython-input-42-58323936ac63>", line 5, in <module>
    pdfkit.from_url("http://www.google.com", 'd:\\out.pdf', configuration=config)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\api.py", line 26, in from_url
    return r.to_pdf(output_path)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\pdfkit\pdfkit.py", line 129, in to_pdf
    stderr=subprocess.PIPE)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 143, in __init__
    super(SubprocessPopen, self).__init__(*args, **kwargs)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 729, in __init__
    restore_signals, start_new_session)

  File "C:\Users\31081\AppData\Local\conda\conda\envs\ml\lib\subprocess.py", line 1017, in _execute_child
    startupinfo)

OSError: [WinError 740] The requested operation requires elevation
u4vypkhs

u4vypkhs1#

我也遇到了这个问题,我通过运行.exe解决了这个问题,运行后它会创建新的目录,进入这个目录然后bin目录,你会发现一个exe文件,如C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf. exe所以,在代码中将“D:\w.exe”替换为上述路径,然后它就会工作

guz6ccqo

guz6ccqo2#

原因

您很可能是因为下载了安装程序版本,但没有安装它而遇到该错误。因此,当在指向该可执行文件的配置中使用wkhtmltopdf运行代码时(这需要提升权限),您会遇到以下错误:

OSError: [WinError 740] The requested operation requires elevation

溶液

您可以通过运行该安装程序版本并选择目标文件夹(例如C:\Program Files)来安装wkhtmltopdf。您将在bin文件夹中找到需要添加到配置中的wkhtmltopdf.exe文件。因此,您应该使用以下命令:

config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf', configuration=config)

另一个解决方案是下载7z Archive版本,解压缩文件,在bin文件夹下也可以找到wkhtmltopdf.exe

nhaq1z21

nhaq1z213#

当我还没有完全安装wkpdftohtml库的时候,我就遇到了这个问题。一旦它被解包,这个运行就不需要提升了。

相关问题