pdf文件加密学习起,pdf 中图片如何提取文本

一、提出问题

在我们的工作中,有时候上级让下级将盖章的文件生成PDF文件通过内部平台发送到上级邮箱,那如何解决呢?是去找一个扫描仪,还是用手机拍图转。用Python基实就能实现。

二、分析问题

现在网上好多的软件都是收费的,转pdf,如何查看文件是否加密,对于图片如何识别文本。

三、docx2pdf模块的使用

1.安装模块

pip install docx2pdf

2.介绍

pdf2docx 是一个Python模块,可以用来将PDF文件转换成Word文档。它是基于Python的pdfminer和python-docx库开发的,可以在Windows、Linux和Mac系统上运行。

四、案例分析

复制代码
import docx2pdf
import os
import PyPDF2

pdf_path = "C:\\Users\\lenovo\\Desktop\\test\\"
files = []
for file in os.listdir(pdf_path):
    if file.endswith(".docx"):
        files.append(pdf_path+file)
        print(files)
for file in files:
    print(file)
    docx2pdf.convert(str(file))
    file_name=file.split(".")[0]
    print(f"{file_name}转换成功!")



for file in os.listdir(pdf_path):
    if file.endswith(".pdf"):
        files.append(pdf_path + file)
        for file in files:
            outfile=file.split('.')[0]+'加密.pdf'

            with open(file, 'rb') as f:
                pdf = PyPDF2.PdfReader(f)
                print(pdf)
                if pdf.is_encrypted:
                    print(f"{file}是加密文件" )
                else:
                    print(f"{file}不是加密文件")
                write = PyPDF2.PdfWriter()
                for i in range(len(pdf.pages)):
                    write.add_page(pdf.pages[i])
                write.encrypt(user_password='123456',owner_pwd="qwerty", use_128bit=True)
                with open('out_file4.pdf',"wb") as f:
                     write.write(f)

五、如何识别图片中文本

复制代码
import tesseract
import os
from PIL import Image
imglist = []
pdf_path = "./"
for file in os.listdir(pdf_path):
    if file.endswith(".png"):
        imglist.append(pdf_path + file)
        print(imglist)
imgtext = []
for img in imglist:
    print(img)
    text = tesseract.image_to_string(Image.open(img))
    imgtext.append(text)
相关推荐
茯苓gao4 小时前
嵌入式开发笔记:CANopen相关移位运算与通信协议术语详解
网络·嵌入式硬件·学习·信息与通信
HERR_QQ7 小时前
强化学习的数学原理 学习笔记
人工智能·笔记·学习·自动驾驶
MartinYeung58 小时前
[论文学习]DP-Fusion:面向大语言模型的令牌级差分隐私推理-深度解析
人工智能·学习·语言模型
zwenqiyu9 小时前
非线性字符串数据结构串讲
数据结构·c++·学习·算法
MartinYeung59 小时前
[论文学习]SecureGate:通过令牌级门控学习何时安全地揭示PII-深度解析
学习·安全
大鱼>10 小时前
超参数调优进阶:Optuna/Bayesian/Early Stopping
人工智能·学习·机器学习·聚类
liuyicenysabel11 小时前
多服务上线日记二:
服务器·笔记·学习
小弥儿11 小时前
GitHub今日热榜 | 2026-07-05:阿里巴巴浏览器Agent上榜
学习·开源·github
‿hhh11 小时前
Dify核心模块详解:从文本生成到智能体
人工智能·学习·microsoft·agent·上下文·记忆
辰海Coding11 小时前
MiniSpring框架学习笔记-动态代理:如何在运行时插入逻辑?
java·笔记·学习·spring·动态代理