網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
為什么會(huì)做這個(gè)?
因?yàn)槲覀儼褀ord轉(zhuǎn)化為pdf,wps默認(rèn)轉(zhuǎn)化為文字pdf,而圖片pdf要會(huì)員。
網(wǎng)上確實(shí)也有網(wǎng)站可以實(shí)現(xiàn)免費(fèi)的,但是未必安全。
思路
我看了網(wǎng)上的很多代碼,都是先把文字pdf先轉(zhuǎn)化為圖片,然后再組裝成pdf文檔。我的思路也是這樣的。
但是我和他們很大的不一樣就是,我不需要先把圖片保存起來(lái)再提取,而我只需要把圖片的信息先存起來(lái),再輸出pdf。
代碼展示
先安裝依賴
pip install PyMuPDF
輸入文件列表
import os import fitz def single_wordpdf_to_imgpdf(pdf_path: str): pdf = fitz.open(pdf_path) # 打開(kāi)pdf目錄 pdf_img = fitz.open() # 打開(kāi)空文件,用來(lái)存圖片pdf for page_inf in pdf: definition = 3 # 清晰度,感覺(jué)輸出的pdf不夠清晰,可以調(diào)大,調(diào)大,文件大小也會(huì)變大 matrix = fitz.Matrix(definition, definition) img = page_inf.get_pixmap(matrix=matrix).tobytes() img = fitz.open("png", img) pdf_bytes = img.convert_to_pdf() pdf_img.insert_pdf(fitz.open("pdf", pdf_bytes)) pdf_img.save("圖片pdf_" + os.path.basename(pdf_path)) def group_wordpdf_to_imgpdf(path_array: list[str]): for pdf_path in path_array: print(pdf_path, "轉(zhuǎn)換中...") single_wordpdf_to_imgpdf(pdf_path) print("完成") if __name__ == '__main__': path = ["xxx.pdf"] group_wordpdf_to_imgpdf(path)
輸入文件夾
import os import fitz import time def single_wordpdf_to_imgpdf(pdf_path: str): pdf = fitz.open(pdf_path) # 打開(kāi)pdf目錄 pdf_img = fitz.open() # 打開(kāi)空文件,用來(lái)存圖片pdf for page_inf in pdf: definition = 3 # 清晰度,感覺(jué)輸出的pdf不夠清晰,可以調(diào)大,調(diào)大,文件大小也會(huì)變大 matrix = fitz.Matrix(definition, definition) img = page_inf.get_pixmap(matrix=matrix).tobytes() img = fitz.open("png", img) pdf_bytes = img.convert_to_pdf() pdf_img.insert_pdf(fitz.open("pdf", pdf_bytes)) if not os.path.exists("output"): os.makedirs("output") # 處理好的pdf存入了output目錄下 # pdf_img.save("output/圖片pdf_" + os.path.basename(pdf_path)) def group_wordpdf_to_imgpdf(path_array: list[str]): for pdf_path in path_array: print(pdf_path, "轉(zhuǎn)換中...") single_wordpdf_to_imgpdf(pdf_path) print("完成") def folder_pdf_files(folder: str) -> list[str]: # 一個(gè)文件夾里面有多少pdf文件 file_list = [] for a, b, c in os.walk(folder): if b == []: for filename in c: if filename[-3:].lower() == 'pdf': file_path = os.path.join(a, filename) file_list.append(file_path) print(folder, ": 有", len(file_list), "個(gè)pdf文件") return file_list if __name__ == '__main__': time_start = time.time() path_list = folder_pdf_files("目錄") group_wordpdf_to_imgpdf(path_list) time_end = time.time() print("程序運(yùn)行時(shí)間:", round(time_end - time_start, 2), "秒")
原文鏈接:https://blog.csdn.net/qq_45019494/article/details/123741012
相關(guān)推薦
- 2022-10-23 Redis?異常?read?error?on?connection?的解決方案_Redis
- 2022-08-04 Python中reduce函數(shù)詳解_python
- 2022-06-16 使用golang如何優(yōu)雅的關(guān)機(jī)或重啟操作示例_Golang
- 2022-04-19 Windows中Python上傳文件到Liunx下的fastdfs
- 2022-05-11 在 WebStorm 中誤添加自定義的 HTML 屬性,如何刪除
- 2022-02-27 Uncaught (in promise) Error: Redirected when going
- 2022-11-01 詳解批處理文件語(yǔ)法_DOS/BAT
- 2022-10-07 Qt入門(mén)學(xué)習(xí)之?dāng)?shù)據(jù)庫(kù)操作指南_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支