Python實(shí)現(xiàn)網(wǎng)頁(yè)文件轉(zhuǎn)PDF文件和PNG圖片的示例代碼
一、html網(wǎng)頁(yè)文件轉(zhuǎn)pdf
#將HTML文件導(dǎo)出為PDF def html_to_pdf(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltopdf.exe'): ''' 將HTML文件導(dǎo)出為PDF :param html_path:str類(lèi)型,目標(biāo)HTML文件的路徑,可以是一個(gè)路徑,也可以是多個(gè)路徑,以list方式傳入路徑;或者一個(gè)或者多個(gè)網(wǎng)址;或者為一個(gè)字符串 :param pdf_path:str類(lèi)型,需要導(dǎo)出的PDF文件的路徑 :param html_encoding:str類(lèi)型,html的編碼格式,具體要看html頁(yè)面到底是以什么編碼格式保存的 :param path_wkpdf:str類(lèi)型,path_wkpdf = r'.\Tools\wkhtmltopdf.exe' # 工具路徑 :return: ''' cfg = pdfkit.configuration(wkhtmltopdf=path_wkpdf) options = { "encoding": html_encoding # 這個(gè)具體要看html頁(yè)面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網(wǎng)址 #從url獲取html,再轉(zhuǎn)為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文件轉(zhuǎn)為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目標(biāo)是否為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: #將字符串轉(zhuǎn)為pdf print('from_string=>pdf') pdfkit.from_string(html_path, pdf_path,options=options, configuration=cfg)
所需要用的附件程序:
wkhtmltopdf.exe
二、html網(wǎng)頁(yè)文件轉(zhuǎn)png
#將HTML文件導(dǎo)出為圖片 def html_to_png(html_path,pdf_path='.\\pdf_new.pdf',html_encoding='UTF-8',path_wkpdf = r'.\Tools\wkhtmltoimage.exe'): ''' 將HTML文件導(dǎo)出為圖片 :param html_path:str類(lèi)型,目標(biāo)HTML文件的路徑,可以是一個(gè)路徑,也可以是多個(gè)路徑,以list方式傳入路徑;或者一個(gè)或者多個(gè)網(wǎng)址;或者為一個(gè)字符串 :param pdf_path:str類(lèi)型,需要導(dǎo)出的圖片文件的路徑 :param html_encoding:str類(lèi)型,html的編碼格式,具體要看html頁(yè)面到底是以什么編碼格式保存的 :param path_wkpdf:str類(lèi)型,path_wkpdf = r'.\Tools\wwkhtmltoimage.exe' # 工具路徑 :return: ''' cfg = imgkit.config(wkhtmltoimage=path_wkpdf) options = { "encoding": html_encoding # 這個(gè)具體要看html頁(yè)面到底是以什么編碼格式保存的 } if 'http' in str(html_path) and ('html' not in str(html_path) or 'HTML' not in str(html_path)): #判斷是否為非網(wǎng)址 #從url獲取html,再轉(zhuǎn)為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文件轉(zhuǎn)為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目標(biāo)是否為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: #將字符串轉(zhuǎn)為pdf print('from_string=>png') imgkit.from_string(html_path, pdf_path,options=options, config=cfg)
所需要用的附件程序:
wkhtmltoimage.exe
到此這篇關(guān)于Python實(shí)現(xiàn)網(wǎng)頁(yè)文件轉(zhuǎn)PDF文件和PNG圖片的示例代碼的文章就介紹到這了,更多相關(guān)Python網(wǎng)頁(yè)文件轉(zhuǎn)PDF PNG內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python爬蟲(chóng)網(wǎng)頁(yè)元素定位術(shù)
- python爬蟲(chóng)lxml庫(kù)解析xpath網(wǎng)頁(yè)過(guò)程示例
- 使用pyscript在網(wǎng)頁(yè)中撰寫(xiě)Python程式的方法
- 一文教會(huì)你用Python獲取網(wǎng)頁(yè)指定內(nèi)容
- Python用requests模塊實(shí)現(xiàn)動(dòng)態(tài)網(wǎng)頁(yè)爬蟲(chóng)
- requests.gPython?用requests.get獲取網(wǎng)頁(yè)內(nèi)容為空?’?’問(wèn)題
- python爬取網(wǎng)頁(yè)數(shù)據(jù)到保存到csv
- 利用Python自制網(wǎng)頁(yè)并實(shí)現(xiàn)一鍵自動(dòng)生成探索性數(shù)據(jù)分析報(bào)告
相關(guān)文章
詳解python中TCP協(xié)議中的粘包問(wèn)題
這篇文章主要介紹了python中TCP協(xié)議中的粘包問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Python數(shù)據(jù)結(jié)構(gòu)之棧、隊(duì)列及二叉樹(shù)定義與用法淺析
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)之棧、隊(duì)列及二叉樹(shù)定義與用法,結(jié)合具體實(shí)例形式分析了Python數(shù)據(jù)結(jié)構(gòu)中棧、隊(duì)列及二叉樹(shù)的定義與使用相關(guān)操作技巧,需要的朋友可以參考下2018-12-12使用Python-OpenCV消除圖像中孤立的小區(qū)域操作
這篇文章主要介紹了使用Python-OpenCV消除圖像中孤立的小區(qū)域操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07解決Python字典查找報(bào)Keyerror的問(wèn)題
這篇文章主要介紹了解決Python字典查找報(bào)Keyerror的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05python機(jī)器學(xué)習(xí)之KNN分類(lèi)算法
這篇文章主要為大家詳細(xì)介紹了python機(jī)器學(xué)習(xí)之KNN分類(lèi)算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08使用matplotlib.pyplot繪制多個(gè)圖片和圖表實(shí)現(xiàn)方式
這篇文章主要介紹了使用matplotlib.pyplot繪制多個(gè)圖片和圖表的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08基于Django ORM、一對(duì)一、一對(duì)多、多對(duì)多的全面講解
今天小編就為大家分享一篇基于Django ORM、一對(duì)一、一對(duì)多、多對(duì)多的全面講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07win10安裝tesserocr配置 Python使用tesserocr識(shí)別字母數(shù)字驗(yàn)證碼
這篇文章主要介紹了win10安裝tesserocr配置 Python使用tesserocr識(shí)別字母數(shù)字驗(yàn)證碼,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01