python實(shí)現(xiàn)pdf轉(zhuǎn)word和excel的示例代碼
一、引言
在辦公中,我們經(jīng)常遇收到pdf文件格式,因?yàn)閜df格式文件不易修改,當(dāng)我們需要編輯這些pdf文件時(shí),經(jīng)常需要開(kāi)通會(huì)員或收費(fèi)功能才能使用編輯功能。今天,我要和大家分享的,是如何使用python編程實(shí)現(xiàn),將PDF文件輕松轉(zhuǎn)換成Word和Excel格式,讓編輯變得輕而易舉。
二、python編程
要將PDF轉(zhuǎn)換為Word,我們需要解析PDF的布局和內(nèi)容,并將其重新格式化為Word文檔。這涉及到復(fù)雜的文本識(shí)別和格式轉(zhuǎn)換技術(shù)。
使用過(guò)如下幾個(gè)庫(kù):最好的還是pdf2docx。
(一)、使用 pdf2docx 庫(kù)
(二)、使用 PyMuPDF 庫(kù)
(三)、使用 pdfplumber 庫(kù)
(四)、使用 PyPDF2 和 python-docx 庫(kù)
重點(diǎn):pdf2docx 是一個(gè)將 PDF 文件轉(zhuǎn)換為 DOCX 文件的 Python 庫(kù)。
pip install pdf2docx -i https://mirrors.aliyun.com/pypi/simple
更換PIP源
PIP源在國(guó)外,速度慢,可以更換為國(guó)內(nèi)源,以下是國(guó)內(nèi)一些常用的PIP源。豆瓣(douban) http://pypi.douban.com/simple/
清華大學(xué) https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云 http://mirrors.aliyun.com/pypi/simple/
中國(guó)科技大學(xué) https://pypi.mirrors.ustc.edu.cn/simple/
中國(guó)科學(xué)技術(shù)大學(xué) http://pypi.mirrors.ustc.edu.cn/simple/
1,PDF轉(zhuǎn)Word
from pdf2docx import Converter
# pdf轉(zhuǎn)word方法
def pdf_to_word(pdf_path, word_path=None, page_nums=None):
'''
@方法名稱(chēng): pdf轉(zhuǎn)word
@中文注釋: pdf轉(zhuǎn)word
@入?yún)?
@param pdf_path str pdf文件路徑
@param page_nums str 頁(yè)碼序號(hào)
@出參:
@返回狀態(tài):
@return 0 失敗或異常
@return 1 成功
@返回錯(cuò)誤碼
@返回錯(cuò)誤信息
@param doc_file str word文件名
@作 者: PandaCode輝
@weixin公眾號(hào): PandaCode輝
@創(chuàng)建時(shí)間: 2024-12-17
@使用范例: pdf_to_word('test.pdf')
'''
global cv
result_dict = {}
try:
if not type(pdf_path) is str:
result_dict["error_code"] = "111111"
result_dict["error_msg"] = "pdf文件路徑參數(shù)類(lèi)型錯(cuò)誤,不為字符串"
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_dict
start_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)
# 初始化轉(zhuǎn)換器
cv = Converter(pdf_path)
# 轉(zhuǎn)換整本PDF或指定頁(yè)碼
if page_nums:
# 解析頁(yè)碼參數(shù)
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)
# 轉(zhuǎn)換指定頁(yè)碼
cv.convert(docx_filename=word_path, pages=pages)
else:
# 轉(zhuǎn)換整本PDF
cv.convert(docx_filename=word_path, start=0)
# 保存為Word文檔
cv.close()
# 識(shí)別時(shí)間
end_time = time.time()
# 計(jì)算耗時(shí)差,單位毫秒
recognize_time = (end_time - start_time) * 1000
# 保留2位小數(shù)
recognize_time = round(recognize_time, 2)
# print('處理時(shí)間:' + str(recognize_time) + '毫秒')
result_dict["recognize_time"] = recognize_time
result_dict["error_code"] = "000000"
result_dict["error_msg"] = "pdf轉(zhuǎn)word成功"
# 使用os.path.basename()獲取文件名
word_file_name = os.path.basename(word_path)
# 打印結(jié)果
# print("文件名:", word_file_name)
result_dict["filename"] = word_file_name
result_dict["file_size_mb"] = file_size_mb
return result_dict
except Exception as e:
cv.close()
print("pdf轉(zhuǎn)word異常," + str(e))
result_dict["error_code"] = "999999"
result_dict["error_msg"] = "PDF到Word轉(zhuǎn)換過(guò)程中發(fā)生錯(cuò)誤," + str(e)
return result_dict2,PDF轉(zhuǎn)Excel
要將PDF轉(zhuǎn)換為Excel,目前沒(méi)有現(xiàn)成的轉(zhuǎn)換庫(kù),需要稍加處理下。
使用過(guò)如下幾個(gè)庫(kù):
(一)、使用 pdf2docx 庫(kù) 和 docx 庫(kù) 和 pandas 庫(kù)
先將pdf轉(zhuǎn)成word文檔,然后讀取word文檔中的表格內(nèi)容,然后再轉(zhuǎn)成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
'''
不擅長(zhǎng)編程的用戶(hù),可以選擇我的免費(fèi)工具箱,開(kāi)箱即用,方便快捷。
print("搜/索/wei/xin/小/程/序: 全能科技工具箱")
'''
# pdf轉(zhuǎn)excel方法
def pdf_to_excel(pdf_path, xlsx_path=None, page_nums=None):
'''
@方法名稱(chēng): pdf轉(zhuǎn)excel
@中文注釋: pdf轉(zhuǎn)excel
@入?yún)?
@param pdf_path str pdf文件路徑
@param page_nums str 頁(yè)碼序號(hào)
@出參:
@返回狀態(tài):
@return 0 失敗或異常
@return 1 成功
@返回錯(cuò)誤碼
@返回錯(cuò)誤信息
@param xlsx_file str excel文件名
@作 者: PandaCode輝
@weixin公眾號(hào): PandaCode輝
@創(chuàng)建時(shí)間: 2025-01-06
@使用范例: pdf_to_excel('test.pdf')
'''
global cv
result_dict = {}
try:
if not type(pdf_path) is str:
result_dict["error_code"] = "111111"
result_dict["error_msg"] = "pdf文件路徑參數(shù)類(lèi)型錯(cuò)誤,不為字符串"
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_dict
start_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轉(zhuǎn)成doc文檔
rsp_dict = pdf_to_word(pdf_path, page_nums=page_nums)
if rsp_dict["error_code"] == "000000":
# 第二步,再讀取doc文檔,轉(zhuǎn)成xlsx文檔
# 打開(kāi)Word文檔
doc = Document(word_path)
if len(doc.tables) < 1:
result_dict["error_code"] = "999999"
result_dict["error_msg"] = "PDF文件未找到表格內(nèi)容,無(wú)法轉(zhuǎn)成xlsx文檔."
return result_dict
# 創(chuàng)建一個(gè)Excel writer對(duì)象
with pd.ExcelWriter(xlsx_path, engine='openpyxl') as writer:
# 遍歷文檔中的所有表格
for i, table in enumerate(doc.tables, start=1):
# 創(chuàng)建一個(gè)空的DataFrame來(lái)存儲(chǔ)表格數(shù)據(jù)
data = []
# 遍歷表格中的所有行
for row in table.rows:
# 遍歷行中的所有單元格
row_data = []
for cell in row.cells:
row_data.append(cell.text)
data.append(row_data)
# 將數(shù)據(jù)轉(zhuǎn)換為DataFrame
df = pd.DataFrame(data)
# 將DataFrame保存到Excel的不同工作表中
sheet_name = f"Table_{i}"
df.to_excel(writer, sheet_name=sheet_name, index=False, header=False)
# print(f"轉(zhuǎn)換完成,結(jié)果保存在{xlsx_path}中。")
else:
result_dict["error_code"] = rsp_dict["error_code"]
result_dict["error_msg"] = rsp_dict["error_msg"]
return result_dict
# 識(shí)別時(shí)間
end_time = time.time()
# 計(jì)算耗時(shí)差,單位毫秒
recognize_time = (end_time - start_time) * 1000
# 保留2位小數(shù)
recognize_time = round(recognize_time, 2)
# print('處理時(shí)間:' + str(recognize_time) + '毫秒')
result_dict["recognize_time"] = recognize_time
result_dict["error_code"] = "000000"
result_dict["error_msg"] = "pdf轉(zhuǎn)excel成功"
# 使用os.path.basename()獲取文件名
xlsx_file_name = os.path.basename(xlsx_path)
result_dict["filename"] = xlsx_file_name
return result_dict
except Exception as e:
print("pdf轉(zhuǎn)excel異常," + str(e))
result_dict["error_code"] = "999999"
result_dict["error_msg"] = "PDF到excel轉(zhuǎn)換過(guò)程中發(fā)生錯(cuò)誤," + str(e)
return result_dict(二)、使用 pdfplumber 和 python-pandas 庫(kù)
使用pdfplumber庫(kù)讀取pdf表格內(nèi)容,然后寫(xiě)入excel表格文檔中。
pip install pdfplumber -i https://mirrors.aliyun.com/pypi/simple
import pandas as pd
import pdfplumber
'''
不擅長(zhǎng)編程的用戶(hù),可以選擇我的免費(fèi)工具箱,開(kāi)箱即用,方便快捷。
print("搜/索/wei/xin/小/程/序: 全能科技工具箱")
'''
def pdf_to_excel_new(pdf_path, xlsx_path=None, page_nums=None):
'''
@方法名稱(chēng): pdf轉(zhuǎn)excel
@中文注釋: pdf轉(zhuǎn)excel
@入?yún)?
@param pdf_path str pdf文件路徑
@param page_nums str 頁(yè)碼序號(hào)
@出參:
@返回狀態(tài):
@return 0 失敗或異常
@return 1 成功
@返回錯(cuò)誤碼
@返回錯(cuò)誤信息
@param xlsx_file str excel文件名
@作 者: PandaCode輝
@weixin公眾號(hào): PandaCode輝
@創(chuàng)建時(shí)間: 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文件路徑參數(shù)類(lèi)型錯(cuò)誤,不為字符串"
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_dict
start_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 中的文本數(shù)據(jù)
with pdfplumber.open(pdf_path) as pdf:
if len(pdf.pages) < 1:
result_dict["error_code"] = "999999"
result_dict["error_msg"] = "PDF文件未找到表格內(nèi)容,無(wú)法轉(zhuǎn)成xlsx文檔."
return result_dict
# 創(chuàng)建一個(gè) Excel 的寫(xiě)入器
with pd.ExcelWriter(xlsx_path) as writer:
# 轉(zhuǎn)換整本PDF或指定頁(yè)碼
if page_nums:
# 解析頁(yè)碼參數(shù)
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)
# 轉(zhuǎn)換指定頁(yè)碼
for i in pages:
page = pdf.pages[i]
# 提取當(dāng)前頁(yè)的表格數(shù)據(jù)
table = page.extract_table()
if table:
# 將表格數(shù)據(jù)轉(zhuǎn)換為 DataFrame
df = pd.DataFrame(table)
# 將 DataFrame 寫(xiě)入 Excel 的不同工作表
df.to_excel(writer, sheet_name=f'Page {i}', index=False)
else:
# 轉(zhuǎn)換整本PDF
for i, page in enumerate(pdf.pages, start=1):
# 提取當(dāng)前頁(yè)的表格數(shù)據(jù)
table = page.extract_table()
if table:
# 將表格數(shù)據(jù)轉(zhuǎn)換為 DataFrame
df = pd.DataFrame(table)
# 將 DataFrame 寫(xiě)入 Excel 的不同工作表
df.to_excel(writer, sheet_name=f'Page {i}', index=False)
# 識(shí)別時(shí)間
end_time = time.time()
# 計(jì)算耗時(shí)差,單位毫秒
recognize_time = (end_time - start_time) * 1000
# 保留2位小數(shù)
recognize_time = round(recognize_time, 2)
# print('處理時(shí)間:' + str(recognize_time) + '毫秒')
result_dict["recognize_time"] = recognize_time
result_dict["error_code"] = "000000"
result_dict["error_msg"] = "pdf轉(zhuǎn)excel成功"
# 使用os.path.basename()獲取文件名
xlsx_file_name = os.path.basename(xlsx_path)
# 打印結(jié)果
# print("文件名:", xlsx_file_name)
result_dict["filename"] = xlsx_file_name
# 獲取文件大?。ㄗ止?jié))
file_size_bytes = os.path.getsize(xlsx_path)
# 將字節(jié)轉(zhuǎn)換為兆字節(jié)
file_size_mb = file_size_bytes / (1024 * 1024)
# 打印結(jié)果
# print("文件大小(兆字節(jié)):", file_size_mb)
result_dict["file_size_mb"] = file_size_mb
return result_dict
except Exception as e:
print("pdf轉(zhuǎn)excel異常," + str(e))
result_dict["error_code"] = "999999"
result_dict["error_msg"] = "PDF到excel轉(zhuǎn)換過(guò)程中發(fā)生錯(cuò)誤," + str(e)
return result_dict三、前端頁(yè)面效果展示
1,選擇PDF文件
2,選擇轉(zhuǎn)換類(lèi)型:PDF轉(zhuǎn)Word 和 PDF轉(zhuǎn)Excel
3,頁(yè)面范圍:可選參數(shù),不選則全部轉(zhuǎn)換

總結(jié)
- pdf2docx 和 PyMuPDF 是pdf轉(zhuǎn)word更直接的選擇,因?yàn)樗鼈儗?zhuān)門(mén)用于轉(zhuǎn)換 PDF 到 DOCX,并且通常在版面還原方面做得更好。
- pdfplumber 更適合于文本和表格的提取,而不是直接的格式轉(zhuǎn)換。
- PyPDF2 和 python-docx 的組合提供了更多的靈活性,但可能需要更多的自定義代碼來(lái)處理復(fù)雜的布局和格式。
根據(jù)你的需求,選擇最適合你的庫(kù)。如果你需要高度保真的版面還原,pdf2docx 或 PyMuPDF 可能是更好的選擇。如果你需要從 PDF 中提取文本和表格數(shù)據(jù),pdfplumber 可能更適合。
到此這篇關(guān)于python實(shí)現(xiàn)pdf轉(zhuǎn)word和excel的文章就介紹到這了,更多相關(guān)python實(shí)現(xiàn)pdf轉(zhuǎn)word和excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用Python實(shí)現(xiàn)某OA系統(tǒng)的自動(dòng)定位功能
這篇文章主要介紹了利用Python實(shí)現(xiàn)某OA系統(tǒng)的自動(dòng)定位功能,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
關(guān)于Python3的import問(wèn)題(pycharm可以運(yùn)行命令行import錯(cuò)誤)
這篇文章主要介紹了關(guān)于Python3的import問(wèn)題(pycharm可以運(yùn)行命令行import錯(cuò)誤),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Python3.4實(shí)現(xiàn)從HTTP代理網(wǎng)站批量獲取代理并篩選的方法示例
這篇文章主要介紹了Python3.4實(shí)現(xiàn)從HTTP代理網(wǎng)站批量獲取代理并篩選的方法,涉及Python網(wǎng)絡(luò)連接、讀取、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Python安裝Gradio和常見(jiàn)安裝問(wèn)題解決辦法
Gradio是一款便捷的Python庫(kù),專(zhuān)門(mén)用于創(chuàng)建機(jī)器學(xué)習(xí)模型的Web應(yīng)用,安裝通常簡(jiǎn)單,但偶爾會(huì)遇到依賴(lài)問(wèn)題或環(huán)境配置錯(cuò)誤,這篇文章主要介紹了Python安裝Gradio和常見(jiàn)安裝問(wèn)題解決辦法,需要的朋友可以參考下2024-10-10
Django中的JWT身份驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了Django中的JWT身份驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲(chóng)實(shí)例
這篇文章主要介紹了基于Python實(shí)現(xiàn)的百度貼吧網(wǎng)絡(luò)爬蟲(chóng),實(shí)例分析了Python實(shí)現(xiàn)網(wǎng)絡(luò)爬蟲(chóng)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
玩轉(zhuǎn)python爬蟲(chóng)之爬取糗事百科段子
這篇文章主要介紹了python爬蟲(chóng)爬取糗事百科段子,詳細(xì)介紹下,如何來(lái)抓取到糗事百科里面的指定內(nèi)容,感興趣的小伙伴們可以參考一下2016-02-02
Python Django框架實(shí)現(xiàn)應(yīng)用添加logging日志操作示例
這篇文章主要介紹了Python Django框架實(shí)現(xiàn)應(yīng)用添加logging日志操作,結(jié)合實(shí)例形式分析了Django框架中添加Python內(nèi)建日志模塊相關(guān)操作技巧,需要的朋友可以參考下2019-05-05

