溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

用Python把pdf轉換成word的方法

發布時間:2020-10-29 09:20:43 來源:億速云 閱讀:264 作者:小新 欄目:編程語言

這篇文章主要介紹用Python把pdf轉換成word的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、下載所需要的庫

1.pdfminer  安裝庫命令 

pip install pdfminer3k

pdfminer3k是pdfminer的Python 3端口。PDFMiner是從PDF文檔中提取信息的工具。與其他PDF相關的工具不同,它完全專注于獲取和分析文本數據。PDFMiner允許獲取頁面中文本的確切位置,以及其他信息,如

字體或線條。它包含一個PDF轉換器,可以將PDF文件轉換為其他文本格式(如HTML)。它有一個可擴展的PDF解析器,可用于其他目的而不是文本分析。

2. docx   安裝庫命令 

pip install python_docx

Python DocX目前是Python OpenXML的一部分,你可以用它打開Word 2007及以后的文檔,而用它保存的文檔可以在Microsoft Office 2007/2010, Microsoft Mac Office 2008, Google Docs, OpenOffice.org 3, and Apple iWork 08中打開。

二、代碼

注意:pdf中非圖片構成的部分才能被成功轉換

1#-*- coding: UTF-8 -*- 2#!/usr/bin/python 3# -*- coding: utf-8 -*- 4 5import sys 6import importlib 7importlib.reload(sys) 8 9from pdfminer.pdfparser import PDFParser,PDFDocument 10from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter 11from pdfminer.converter import PDFPageAggregator 12from pdfminer.layout import * 13from pdfminer.pdfinterp import PDFTextExtractionNotAllowed 14import os 15 16#設置工作目錄文件夾 17os.chdir(r'c:/users/dicey/desktop/codes/pdf-docx') 18 19''' 20解析pdf文件,獲取文件中包含的各種對象 21''' 22# 解析pdf文件函數 23def parse(pdf_path): 24    fp = open('diya.pdf', 'rb')  # 以二進制讀模式打開 25    # 用文件對象來創建一個pdf文檔分析器 26    parser = PDFParser(fp) 27    # 創建一個PDF文檔 28    doc = PDFDocument() 29    # 連接分析器 與文檔對象 30    parser.set_document(doc) 31    doc.set_parser(parser) 32 33    # 提供初始化密碼 34    # 如果沒有密碼 就創建一個空的字符串 35    doc.initialize() 36 37    # 檢測文檔是否提供txt轉換,不提供就忽略 38    if not doc.is_extractable: 39        raise PDFTextExtractionNotAllowed 40    else: 41        # 創建PDf 資源管理器 來管理共享資源 42        rsrcmgr = PDFResourceManager() 43        # 創建一個PDF設備對象 44        laparams = LAParams() 45        device = PDFPageAggregator(rsrcmgr, laparams=laparams) 46        # 創建一個PDF解釋器對象 47        interpreter = PDFPageInterpreter(rsrcmgr, device) 48 49        # 用來計數頁面,圖片,曲線,figure,水平文本框等對象的數量 50        num_page, num_image, num_curve, num_figure, num_TextBoxHorizontal = 0, 0, 0, 0, 0 51 52        # 循環遍歷列表,每次處理一個page的內容 53        for page in doc.get_pages(): # doc.get_pages() 獲取page列表 54            num_page += 1  # 頁面增一 55            interpreter.process_page(page) 56            # 接受該頁面的LTPage對象 57            layout = device.get_result() 58            for x in layout: 59                if isinstance(x,LTImage):  # 圖片對象 60                    num_image += 1 61                if isinstance(x,LTCurve):  # 曲線對象 62                    num_curve += 1 63                if isinstance(x,LTFigure):  # figure對象 64                    num_figure += 1 65                if isinstance(x, LTTextBoxHorizontal):  # 獲取文本內容 66                    num_TextBoxHorizontal += 1  # 水平文本框對象增一 67                    # 保存文本內容 68                    with open(r'test2.doc', 'a',encoding='utf-8') as f:    #生成doc文件的文件名及路徑 69                        results = x.get_text() 70                        f.write(results) 71                        f.write('\n') 72        print('對象數量:\n','頁面數:%s\n'%num_page,'圖片數:%s\n'%num_image,'曲線數:%s\n'%num_curve,'水平文本框:%s\n' 73              %num_TextBoxHorizontal) 74 75 76if __name__ == '__main__': 77    pdf_path = r'diya.pdf'  #pdf文件路徑及文件名 78    parse(pdf_path)

以上是用Python把pdf轉換成word的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女