Python OSError: wkhtmltopdf reported an error:Exit with code 1 due to network error:ProtocolUnknownE

Python-Markdown转PDF-报错OSError: wkhtmltopdf reported an error:Exit with code 1 due to network error: ProtocolUnknownError 解决办法

一、问题

在使用 python将markdown格式文件转为pdf时,出现OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError的错误

在这里插入图片描述

OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError

在这里插入图片描述

上面的错误信息提示了是使用wkhtmltopdf时出错

(因为md转pdf需要用到wkhtmltopdf.exe,相关部分代码如下)

在这里插入图片描述


二、原因

本地文件访问权限被禁止了,所以在调用时才报错


三、解决办法

在python的程序中打开本地文件访问权限即可,主要添加"enable-local-file-access":True

权限打开前:

# HTML转PDF
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8'})

权限打开后:

# HTML转PDF
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8',"enable-local-file-access":True})

总结:

# HTML转PDF
# 本地访问权限未打开
# pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8'})
# "enable-local-file-access":True 打开本地文件访问权限
pdfkit.from_string(html, output_path=output, configuration=configuration, options={'encoding': 'utf-8',"enable-local-file-access":True})

成功运行:

在这里插入图片描述

md格式:

在这里插入图片描述

pdf格式:

在这里插入图片描述

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

索子也敲代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值