太贊了!將Python代碼轉(zhuǎn)化為可執(zhí)行的程序
↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能
后臺(tái)回復(fù)【大禮包】送你Python自學(xué)大禮包
PDF文檔遵循一定的規(guī)范,例如精確定位了每個(gè)字符出現(xiàn)在頁面上的坐標(biāo)、根據(jù)坐標(biāo)繪制的各種形狀(線、矩形、曲線等)。所以,用PDF格式傳輸和打印文檔可以保證格式的一致性,不會(huì)像Word那樣因?yàn)殇秩疽娴牟煌霈F(xiàn)格式錯(cuò)亂、多頁少頁等問題。
pip?install?pdf2docx
pdf2docx使用
from?pdf2docx?import?Converter獲取pdf文件路徑。 過濾出當(dāng)前文件夾中所有的pdf文件。 提取pdf文件名和后綴名。 文件名+'docx'拼接重組word文件(改變格式不變文件名)。 使用pdf2docx進(jìn)行文件轉(zhuǎn)換。
import?os
from?pdf2docx?import?Converter
def?pdf_docx():
????#?獲取當(dāng)前工作目錄
????file_path?=?os.getcwd()
????#?遍歷所有文件
????for?file?in?os.listdir(file_path):
????????#?獲取文件后綴
????????suff_name?=?os.path.splitext(file)[1]
????????#?過濾非pdf格式文件
????????if?suff_name?!=?'.pdf':
????????????continue
????????#?獲取文件名稱
????????file_name?=?os.path.splitext(file)[0]
????????#?pdf文件名稱
????????pdf_name?=?os.getcwd()?+?'\\'?+?file
????????#?要轉(zhuǎn)換的docx文件名稱
????????docx_name?=?os.getcwd()?+?'\\'?+?file_name?+?'.docx'
????????#?加載pdf文檔
????????cv?=?Converter(pdf_name)
????????cv.convert(docx_name)
????????cv.close()測(cè)試
我們準(zhǔn)備的pdf文檔有格式,有圖片。先來進(jìn)行測(cè)試

控制臺(tái)信息打印如下,0.17秒完成了3頁pdf->docx文件的轉(zhuǎn)換
[INFO]?Start?to?convert?E:\Python\pycharm++\GOGO數(shù)據(jù)\盧本偉.pdf
[INFO]?[1/4]?Opening?document...
[INFO]?[2/4]?Analyzing?document...
[WARNING]?Replace?font?"MicrosoftYaHeiUI"?with?"Times?New?Roman"?due?to?lack?of?data.
Deprecation:?'getText'?removed?from?class?'Page'?after?v1.19.0?-?use?'get_text'.
Deprecation:?'getImageList'?removed?from?class?'Page'?after?v1.19.0?-?use?'get_images'.
Deprecation:?'getImageBbox'?removed?from?class?'Page'?after?v1.19.0?-?use?'get_image_bbox'.
Deprecation:?'getPNGData'?removed?from?class?'Pixmap'?after?v1.19.0?-?use?'tobytes'.
Deprecation:?'getDrawings'?removed?from?class?'Page'?after?v1.19.0?-?use?'get_drawings'.
Deprecation:?'getLinks'?removed?from?class?'Page'?after?v1.19.0?-?use?'get_links'.
Deprecation:?'getArea'?removed?from?class?'Rect'?after?v1.19.0?-?use?'get_area'.
[INFO]?[3/4]?Parsing?pages...
[INFO]?(1/3)?Page?1
[INFO]?(2/3)?Page?2
[INFO]?(3/3)?Page?3
[INFO]?[4/4]?Creating?pages...
[INFO]?(1/3)?Page?1
[INFO]?(2/3)?Page?2
[INFO]?(3/3)?Page?3
[INFO]?Terminated?in?0.17s.轉(zhuǎn)換完成后的docx文件格式如下:

現(xiàn)在我們已經(jīng)完成了pdf轉(zhuǎn)word的操作,這樣的局限性太大了,萬一我的pc沒有python環(huán)境怎么搞?
pip?install?pyinstaller?
詳細(xì)步驟


執(zhí)行完畢會(huì)發(fā)現(xiàn)生成了 3 個(gè)文件夾

其中 dist 文件夾就有我們已經(jīng)打包完成的 exe 文件。

3、雙擊 exe 就可以運(yùn)行成功了。一鍵抓換pdf-word
夠方便的吧~~

今天的分享就到此結(jié)束啦,
評(píng)論
圖片
表情


