一、引言
在辦公中,我們經常遇收到pdf文件格式,因為pdf格式文件不易修改,當我們需要編輯這些pdf文件時,經常需要開通會員或收費功能才能使用編輯功能。今天,我要和大家分享的,是如何使用python編程實現,將PDF文件輕松轉換成Word和Excel格式,讓編輯變得輕而易舉。
二、python編程
要將PDF轉換為Word,我們需要解析PDF的布局和內容,并將其重新格式化為Word文檔。這涉及到復雜的文本識別和格式轉換技術。
使用過如下幾個庫:最好的還是pdf2docx。
(一)、使用 pdf2docx 庫
(二)、使用 PyMuPDF 庫
(三)、使用 pdfplumber 庫
(四)、使用 PyPDF2 和 python-docx 庫
重點:pdf2docx 是一個將 PDF 文件轉換為 DOCX 文件的 Python 庫。
pip install pdf2docx -i https://mirrors.aliyun.com/pypi/simple
更換PIP源
PIP源在國外,速度慢,可以更換為國內源,以下是國內一些常用的PIP源。豆瓣(douban) http://pypi.douban.com/simple/
清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云 http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/
1,PDF轉Word
from pdf2docx import Converter# pdf轉word方法
def pdf_to_word(pdf_path, word_path=None, page_nums=None):'''@方法名稱: pdf轉word@中文注釋: pdf轉word@入參:@param pdf_path str pdf文件路徑@param page_nums str 頁碼序號@出參:@返回狀態:@return 0 失敗或異常@return 1 成功@返回錯誤碼@返回錯誤信息@param doc_file str word文件名@作 者: PandaCode輝@weixin公眾號: PandaCode輝@創建時間: 2024-12-17@使用范例: pdf_to_word('test.pdf')'''global cvresult_dict = {}try:if not type(pdf_path) is str:result_dict["error_code"] = "111111"result_dict["error_msg"] = "pdf文件路徑參數類型錯誤,不為字符串"return result_dict# 檢查PDF文件是否存在if not os.path.isfile(pdf_path):result_dict["error_code"] = "999999"result_dict["error_msg"] = f"PDF文件未找到: {pdf_path}"return result_dictstart_time = time.time()if not word_path:# 使用os.path.basename()獲取文件名file_path = os.path.dirname(pdf_path)# 使用os.path.basename()獲取文件名file_name = os.path.basename(pdf_path)# 提取文件名,去除文件后綴file_name = file_name.split('.')[0]# print(file_name)# word文件名+路徑word_path = os.path.join(file_path, f'{file_name}.docx')# print(word_path)# 初始化轉換器cv = Converter(pdf_path)# 轉換整本PDF或指定頁碼if page_nums:# 解析頁碼參數pages = []for part in page_nums.split(','):if '-' in part:start, end = part.split('-')pages.extend(range(int(start) - 1, int(end)))else:pages.append(int(part) - 1)# 轉換指定頁碼cv.convert(docx_filename=word_path, pages=pages)else:# 轉換整本PDFcv.convert(docx_filename=word_path, start=0)# 保存為Word文檔cv.close()# 識別時間end_time = time.time()# 計算耗時差,單位毫秒recognize_time = (end_time - start_time) * 1000# 保留2位小數recognize_time = round(recognize_time, 2)# print('處理時間:' + str(recognize_time) + '毫秒')result_dict["recognize_time"] = recognize_timeresult_dict["error_code"] = "000000"result_dict["error_msg"] = "pdf轉word成功"# 使用os.path.basename()獲取文件名word_file_name = os.path.basename(word_path)# 打印結果# print("文件名:", word_file_name)result_dict["filename"] = word_file_nameresult_dict["file_size_mb"] = file_size_mbreturn result_dictexcept Exception as e:cv.close()print("pdf轉word異常," + str(e))result_dict["error_code"] = "999999"result_dict["error_msg"] = "PDF到Word轉換過程中發生錯誤," + str(e)return result_dict
2,PDF轉Excel
要將PDF轉換為Excel,目前沒有現成的轉換庫,需要稍加處理下。
使用過如下幾個庫:
(一)、使用 pdf2docx 庫 和 docx 庫 和 pandas 庫
先將pdf轉成word文檔,然后讀取word文檔中的表格內容,然后再轉成excel文檔。
?
pip install python-docx -i https://mirrors.aliyun.com/pypi/simple
pip install pandas -i https://mirrors.aliyun.com/pypi/simple
from docx import Document
import pandas as pd
'''
不擅長編程的用戶,可以選擇我的免費工具箱,開箱即用,方便快捷。
print("搜/索/wei/xin/小/程/序: 全能科技工具箱")
'''
# pdf轉excel方法
def pdf_to_excel(pdf_path, xlsx_path=None, page_nums=None):'''@方法名稱: pdf轉excel@中文注釋: pdf轉excel@入參:@param pdf_path str pdf文件路徑@param page_nums str 頁碼序號@出參:@返回狀態:@return 0 失敗或異常@return 1 成功@返回錯誤碼@返回錯誤信息@param xlsx_file str excel文件名@作 者: PandaCode輝@weixin公眾號: PandaCode輝@創建時間: 2025-01-06@使用范例: pdf_to_excel('test.pdf')'''global cvresult_dict = {}try:if not type(pdf_path) is str:result_dict["error_code"] = "111111"result_dict["error_msg"] = "pdf文件路徑參數類型錯誤,不為字符串"return result_dict# 檢查PDF文件是否存在if not os.path.isfile(pdf_path):result_dict["error_code"] = "999999"result_dict["error_msg"] = f"PDF文件未找到: {pdf_path}"return result_dictstart_time = time.time()# 使用os.path.basename()獲取文件名file_path = os.path.dirname(pdf_path)# 使用os.path.basename()獲取文件名file_name = os.path.basename(pdf_path)# 提取文件名,去除文件后綴file_name = file_name.split('.')[0]# print(file_name)# word文件名+路徑word_path = os.path.join(file_path, f'{file_name}.docx')# print(word_path)if not xlsx_path:# xlsx文件名+路徑xlsx_path = os.path.join(file_path, f'{file_name}.xlsx')# print(xlsx_path)# 第一步,先將pdf轉成doc文檔rsp_dict = pdf_to_word(pdf_path, page_nums=page_nums)if rsp_dict["error_code"] == "000000":# 第二步,再讀取doc文檔,轉成xlsx文檔# 打開Word文檔doc = Document(word_path)if len(doc.tables) < 1:result_dict["error_code"] = "999999"result_dict["error_msg"] = "PDF文件未找到表格內容,無法轉成xlsx文檔."return result_dict# 創建一個Excel writer對象with pd.ExcelWriter(xlsx_path, engine='openpyxl') as writer:# 遍歷文檔中的所有表格for i, table in enumerate(doc.tables, start=1):# 創建一個空的DataFrame來存儲表格數據data = []# 遍歷表格中的所有行for row in table.rows:# 遍歷行中的所有單元格row_data = []for cell in row.cells:row_data.append(cell.text)data.append(row_data)# 將數據轉換為DataFramedf = pd.DataFrame(data)# 將DataFrame保存到Excel的不同工作表中sheet_name = f"Table_{i}"df.to_excel(writer, sheet_name=sheet_name, index=False, header=False)# print(f"轉換完成,結果保存在{xlsx_path}中。")else:result_dict["error_code"] = rsp_dict["error_code"]result_dict["error_msg"] = rsp_dict["error_msg"]return result_dict# 識別時間end_time = time.time()# 計算耗時差,單位毫秒recognize_time = (end_time - start_time) * 1000# 保留2位小數recognize_time = round(recognize_time, 2)# print('處理時間:' + str(recognize_time) + '毫秒')result_dict["recognize_time"] = recognize_timeresult_dict["error_code"] = "000000"result_dict["error_msg"] = "pdf轉excel成功"# 使用os.path.basename()獲取文件名xlsx_file_name = os.path.basename(xlsx_path)result_dict["filename"] = xlsx_file_namereturn result_dictexcept Exception as e:print("pdf轉excel異常," + str(e))result_dict["error_code"] = "999999"result_dict["error_msg"] = "PDF到excel轉換過程中發生錯誤," + str(e)return result_dict
(二)、使用 pdfplumber 和 python-pandas 庫
使用pdfplumber庫讀取pdf表格內容,然后寫入excel表格文檔中。
pip install pdfplumber -i https://mirrors.aliyun.com/pypi/simple
import pandas as pd
import pdfplumber'''
不擅長編程的用戶,可以選擇我的免費工具箱,開箱即用,方便快捷。
print("搜/索/wei/xin/小/程/序: 全能科技工具箱")
'''def pdf_to_excel_new(pdf_path, xlsx_path=None, page_nums=None):'''@方法名稱: pdf轉excel@中文注釋: pdf轉excel@入參:@param pdf_path str pdf文件路徑@param page_nums str 頁碼序號@出參:@返回狀態:@return 0 失敗或異常@return 1 成功@返回錯誤碼@返回錯誤信息@param xlsx_file str excel文件名@作 者: PandaCode輝@weixin公眾號: PandaCode輝@創建時間: 2025-01-06@使用范例: pdf_to_excel('test.pdf')'''result_dict = {}try:if not type(pdf_path) is str:result_dict["error_code"] = "111111"result_dict["error_msg"] = "pdf文件路徑參數類型錯誤,不為字符串"return result_dict# 檢查PDF文件是否存在if not os.path.isfile(pdf_path):result_dict["error_code"] = "999999"result_dict["error_msg"] = f"PDF文件未找到: {pdf_path}"return result_dictstart_time = time.time()# 使用os.path.basename()獲取文件名file_path = os.path.dirname(pdf_path)# 使用os.path.basename()獲取文件名file_name = os.path.basename(pdf_path)# 提取文件名,去除文件后綴file_name = file_name.split('.')[0]# print(file_name)if not xlsx_path:# xlsx文件名+路徑xlsx_path = os.path.join(file_path, f'{file_name}.xlsx')# print(xlsx_path)# 提取 PDF 中的文本數據with pdfplumber.open(pdf_path) as pdf:if len(pdf.pages) < 1:result_dict["error_code"] = "999999"result_dict["error_msg"] = "PDF文件未找到表格內容,無法轉成xlsx文檔."return result_dict# 創建一個 Excel 的寫入器with pd.ExcelWriter(xlsx_path) as writer:# 轉換整本PDF或指定頁碼if page_nums:# 解析頁碼參數pages = []for part in page_nums.split(','):if '-' in part:start, end = part.split('-')pages.extend(range(int(start) - 1, int(end)))else:pages.append(int(part) - 1)# 轉換指定頁碼for i in pages:page = pdf.pages[i]# 提取當前頁的表格數據table = page.extract_table()if table:# 將表格數據轉換為 DataFramedf = pd.DataFrame(table)# 將 DataFrame 寫入 Excel 的不同工作表df.to_excel(writer, sheet_name=f'Page {i}', index=False)else:# 轉換整本PDFfor i, page in enumerate(pdf.pages, start=1):# 提取當前頁的表格數據table = page.extract_table()if table:# 將表格數據轉換為 DataFramedf = pd.DataFrame(table)# 將 DataFrame 寫入 Excel 的不同工作表df.to_excel(writer, sheet_name=f'Page {i}', index=False)# 識別時間end_time = time.time()# 計算耗時差,單位毫秒recognize_time = (end_time - start_time) * 1000# 保留2位小數recognize_time = round(recognize_time, 2)# print('處理時間:' + str(recognize_time) + '毫秒')result_dict["recognize_time"] = recognize_timeresult_dict["error_code"] = "000000"result_dict["error_msg"] = "pdf轉excel成功"# 使用os.path.basename()獲取文件名xlsx_file_name = os.path.basename(xlsx_path)# 打印結果# print("文件名:", xlsx_file_name)result_dict["filename"] = xlsx_file_name# 獲取文件大小(字節)file_size_bytes = os.path.getsize(xlsx_path)# 將字節轉換為兆字節file_size_mb = file_size_bytes / (1024 * 1024)# 打印結果# print("文件大小(兆字節):", file_size_mb)result_dict["file_size_mb"] = file_size_mbreturn result_dictexcept Exception as e:print("pdf轉excel異常," + str(e))result_dict["error_code"] = "999999"result_dict["error_msg"] = "PDF到excel轉換過程中發生錯誤," + str(e)return result_dict
三、前端頁面效果展示
1,選擇PDF文件
2,選擇轉換類型:PDF轉Word 和?PDF轉Excel
3,頁面范圍:可選參數,不選則全部轉換
總結
- pdf2docx?和?PyMuPDF?是pdf轉word更直接的選擇,因為它們專門用于轉換 PDF 到 DOCX,并且通常在版面還原方面做得更好。
- pdfplumber?更適合于文本和表格的提取,而不是直接的格式轉換。
- PyPDF2?和?python-docx?的組合提供了更多的靈活性,但可能需要更多的自定義代碼來處理復雜的布局和格式。
根據你的需求,選擇最適合你的庫。如果你需要高度保真的版面還原,pdf2docx?或?PyMuPDF?可能是更好的選擇。如果你需要從 PDF 中提取文本和表格數據,pdfplumber?可能更適合。
?
??