zoukankan      html  css  js  c++  java
  • python将html文件转换为pdf

    目录下的html文件转换为pdf

    #coding=utf-8
    import os
    import pdfkit
    import filetype
    import _thread
    import time
    import shutil


    dir='C:\Users\Administrator\Desktop\SQL必知必会\' #html文件所在位置
    pd_dir='C:\Users\Administrator\Desktop\mysql学习\SQL必知必会' #输出pdf位置
    path_wkthmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe' #wkhtmltopdf工具下载界面:https://wkhtmltopdf.org/downloads.html
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    txt_name=[]
    dir_name = []

    def file_name():
    for dirpath, dirname, filenames in os.walk(dir, topdown=False):
    for name in filenames:
    cc=os.path.join(dirpath,name)
    dir_name.append(cc)
    txt_name.append(name)
    #判断文件后缀,分别进行处理
    def get_file_type():
    html_n=[]
    pdf_n=[]
    for i in dir_name:
    aaa = os.path.splitext(i)[-1]
    if aaa == '.html':
    html_n.append(i)
    if aaa == '.pdf' or aaa=='.htmlpdf':
    pdf_n.append(i)
    for p in pdf_n:
    os.remove(p)
    for h in html_n:
    ppp=h+'.pdf'
    pdfkit.from_file(h, output_path=ppp, configuration=config)
    shutil.copy(ppp,pd_dir)

    if __name__ == '__main__':
    file_name()
    get_file_type()
  • 相关阅读:
    Mybaits利用插件generator自动生成sql
    idea设置
    centos虚拟机的相关知识
    nginx部署
    apollo配置
    分布式报错解决方案
    搭建springconfigCenter遇到的坑
    idea中git配置
    SpringCloudDay1_概念
    03.2020 MB SD Connect C4 Software “set_SDconnect.bat” File Lost Error Solution
  • 原文地址:https://www.cnblogs.com/byfboke/p/14524370.html
Copyright © 2011-2022 走看看