Python實現(xiàn)網頁文件轉PDF文件和PNG圖片的示例代碼
更新時間:2022年01月13日 09:59:14 作者:幸福的達哥
這篇文章主要介紹了如何利用Python分別實現(xiàn)網頁文件轉為PDF文件和網頁文件轉PNG圖片的示例代碼,文中的代碼簡潔易懂,感興趣的可以動手試試
一、html網頁文件轉pdf
#將HTML文件導出為PDF def html_to_pdf(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltopdf.exe'): ''' 將HTML文件導出為PDF :param html_path:str類型,目標HTML文件的路徑,可以是一個路徑,也可以是多個路徑,以list方式傳入路徑;或者一個或者多個網址;或者為一個字符串 :param pdf_path:str類型,需要導出的PDF文件的路徑 :param html_encoding:str類型,html的編碼格式,具體要看html頁面到底是以什么編碼格式保存的 :param path_wkpdf:str類型,path_wkpdf = r'.\Tools\wkhtmltopdf.exe' # 工具路徑 :return: ''' cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf) options = { "encoding": html_encoding # 這個具體要看html頁面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網址 #從url獲取html,再轉為pdf print('http=>pdf') # pdfkit.from_url('https://httpbin.org/ip', 'ip.pdf', options=options, configuration=cfg) # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.pdf', options=options,configuration=cfg) # 傳入列表 pdfkit.from_url(html_path, pdf_path, options=options, configuration=cfg) elif 'html' in str(html_path) or 'HTML' in str(html_path): #判斷是否為HTML文件 #將html文件轉為pdf print('html,str=>pdf') # pdfkit.from_file(r'./helloworld.html', 'helloworld.pdf',options=options, configuration=cfg) pdfkit.from_file(html_path, pdf_path, options=options, configuration=cfg) elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)): #判斷html目標是否為list, # 如:[r'./helloworld.html', r'./111.html', r'./222.html'] print('html,list=>pdf') pdfkit.from_file(html_path, pdf_path,options=options, configuration=cfg) # 傳入列表 else: #將字符串轉為pdf print('from_string=>pdf') pdfkit.from_string(html_path, pdf_path,options=options, configuration=cfg)
所需要用的附件程序:
wkhtmltopdf.exe
二、html網頁文件轉png
#將HTML文件導出為圖片 def html_to_png(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltoimage.exe'): ''' 將HTML文件導出為圖片 :param html_path:str類型,目標HTML文件的路徑,可以是一個路徑,也可以是多個路徑,以list方式傳入路徑;或者一個或者多個網址;或者為一個字符串 :param pdf_path:str類型,需要導出的圖片文件的路徑 :param html_encoding:str類型,html的編碼格式,具體要看html頁面到底是以什么編碼格式保存的 :param path_wkpdf:str類型,path_wkpdf = r'.\Tools\wwkhtmltoimage.exe' # 工具路徑 :return: ''' cfg = imgkit.config(wkhtmltoimage=path_wkpdf) options = { "encoding": html_encoding # 這個具體要看html頁面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網址 #從url獲取html,再轉為pdf print('http=>png') # pdfkit.from_url('https://httpbin.org/ip', 'ip.png', options=options, configuration=cfg) # pdfkit.from_url(['https://httpbin.org/ip', 'https://httpbin.org/ip'], 'ip.png', options=options,configuration=cfg) # 傳入列表 imgkit.from_url(html_path, pdf_path, options=options, config=cfg) elif 'html' in str(html_path) or 'HTML' in str(html_path): #判斷是否為HTML文件 #將html文件轉為pdf print('html,str=>png') # pdfkit.from_file(r'./helloworld.html', 'helloworld.png',options=options, configuration=cfg) imgkit.from_file(html_path, pdf_path, options=options, config=cfg) elif isinstance(html_path, list) and ('html' in str(html_path) or 'HTML' in str(html_path)): #判斷html目標是否為list, # 如:[r'./helloworld.html', r'./111.html', r'./222.html'] print('html,list=>png') imgkit.from_file(html_path, pdf_path,options=options, config=cfg) # 傳入列表 else: #將字符串轉為pdf print('from_string=>png') imgkit.from_string(html_path, pdf_path,options=options, config=cfg)
所需要用的附件程序:
wkhtmltoimage.exe
到此這篇關于Python實現(xiàn)網頁文件轉PDF文件和PNG圖片的示例代碼的文章就介紹到這了,更多相關Python網頁文件轉PDF PNG內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python數(shù)據(jù)結構之棧、隊列及二叉樹定義與用法淺析
這篇文章主要介紹了Python數(shù)據(jù)結構之棧、隊列及二叉樹定義與用法,結合具體實例形式分析了Python數(shù)據(jù)結構中棧、隊列及二叉樹的定義與使用相關操作技巧,需要的朋友可以參考下2018-12-12使用Python-OpenCV消除圖像中孤立的小區(qū)域操作
這篇文章主要介紹了使用Python-OpenCV消除圖像中孤立的小區(qū)域操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07使用matplotlib.pyplot繪制多個圖片和圖表實現(xiàn)方式
這篇文章主要介紹了使用matplotlib.pyplot繪制多個圖片和圖表的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08win10安裝tesserocr配置 Python使用tesserocr識別字母數(shù)字驗證碼
這篇文章主要介紹了win10安裝tesserocr配置 Python使用tesserocr識別字母數(shù)字驗證碼,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01