您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用Python寫PDF轉(zhuǎn)換器”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
想必小伙伴都經(jīng)歷過,當(dāng)你想要把PDF轉(zhuǎn)為WORD時,自己打字赫赫甩在你眼前:
不充錢就想白嫖??想得美~
然而,博主是不會退縮的,畢竟迎難而上是傳統(tǒng)美德。于是,今天的主題出來了:用python寫一個PDF轉(zhuǎn)WORD的小工具(基于某網(wǎng)站接口)。
網(wǎng)上一搜,你可以發(fā)現(xiàn)很多PDF轉(zhuǎn)換的工具,其中不乏在線轉(zhuǎn)換的網(wǎng)站,比如這樣的:
那么,通過網(wǎng)站提供的測試接口,我們便可以通過爬蟲模擬的方式實現(xiàn)轉(zhuǎn)換。
沒有錯了~思路就是如此的簡單明了,今天的主角便是:
https://app.xunjiepdf.com
通過抓包分析,知道這是一個POST請求,接下來用requests庫模擬即可。
需要注意的是,這個接口僅用于測試,所以可供轉(zhuǎn)換的頁面等都有所限制,如需更完整的功能還請支持原版。
正所謂一萬個coders,就有一萬種codes,以下為我的代碼,僅供參考。
導(dǎo)入相關(guān)庫:
import time import requests
定義PDF2Word類:
#2020最新python學(xué)習(xí)資源分享:1156465813 class PDF2Word(): def __init__(self): self.machineid = 'ccc052ee5200088b92342303c4ea9399' self.token = '' self.guid = '' self.keytag = '' def produceToken(self): url = 'https://app.xunjiepdf.com/api/producetoken' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'Origin': 'https://app.xunjiepdf.com', 'Connection': 'keep-alive', 'Referer': 'https://app.xunjiepdf.com/pdf2word/',} data = {'machineid':self.machineid} res = requests.post(url,headers=headers,data=data) res_json = res.json() if res_json['code'] == 10000: self.token = res_json['token'] self.guid = res_json['guid'] print('成功獲取token') return True else: return False def uploadPDF(self,filepath): filename = filepath.split('/')[-1] files = {'file': open(filepath,'rb')} url = 'https://app.xunjiepdf.com/api/Upload' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0', 'Accept': '*/*', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Content-Type': 'application/pdf', 'Origin': 'https://app.xunjiepdf.com', 'Connection': 'keep-alive', 'Referer': 'https://app.xunjiepdf.com/pdf2word/',} params = ( ('tasktype', 'pdf2word'), ('phonenumber', ''), ('loginkey', ''), ('machineid', self.machineid), ('token', self.token), ('limitsize', '2048'), ('pdfname', filename), ('queuekey', self.guid), ('uploadtime', ''), ('filecount', '1'), ('fileindex', '1'), ('pagerange', 'all'), ('picturequality', ''), ('outputfileextension', 'docx'), ('picturerotate', '0,undefined'), ('filesequence', '0,undefined'), ('filepwd', ''), ('iconsize', ''), ('picturetoonepdf', ''), ('isshare', '0'), ('softname', 'pdfonlineconverter'), ('softversion', 'V5.0'), ('validpagescount', '20'), ('limituse', '1'), ('filespwdlist', ''), ('fileCountwater', '1'), ('languagefrom', ''), ('languageto', ''), ('cadverchose', ''), ('pictureforecolor', ''), ('picturebackcolor', ''), ('id', 'WU_FILE_1'), ('name', filename), ('type', 'application/pdf'), ('lastModifiedDate', ''), ('size', ''),) res= requests.post(url,headers=headers,params=params,files=files) res_json = res.json() if res_json['message'] == '上傳成功': self.keytag = res_json['keytag'] print('成功上傳PDF') return True else: return False def progress(self): url = 'https://app.xunjiepdf.com/api/Progress' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0', 'Accept': 'text/plain, */*; q=0.01', 'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'X-Requested-With': 'XMLHttpRequest', 'Origin': 'https://app.xunjiepdf.com', 'Connection': 'keep-alive', 'Referer': 'https://app.xunjiepdf.com/pdf2word/',} data = { 'tasktag': self.keytag, 'phonenumber': '', 'loginkey': '', 'limituse': '1'} res= requests.post(url,headers=headers,data=data) res_json = res.json() if res_json['message'] == '處理成功': print('PDF處理完成') return True else: print('PDF處理中') return False def downloadWord(self,output): url = 'https://app.xunjiepdf.com/download/fileid/%s'%self.keytag res = requests.get(url) with open(output,'wb') as f: f.write(res.content) print('PDF下載成功("%s")'%output) def convertPDF(self,filepath,outpath): filename = filepath.split('/')[-1] filename = filename.split('.')[0]+'.docx' self.produceToken() self.uploadPDF(filepath) while True: res = self.progress() if res == True: break time.sleep(1) self.downloadWord(outpath+filename)
執(zhí)行主函數(shù):
if __name__=='__main__': pdf2word = PDF2Word() pdf2word.convertPDF('001.pdf','')
注意:convertPDF函數(shù)有兩個參數(shù),第一個為需要轉(zhuǎn)換的PDF,第二個參數(shù)為轉(zhuǎn)換后的目錄。
run一下,一鍵入魂,".docx"文件已經(jīng)躺在了我的目錄中,舒服了~
“怎么用Python寫PDF轉(zhuǎn)換器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。