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()

 

posted @ 2021-03-12 15:47  天宇星空  阅读(742)  评论(0编辑  收藏  举报