Python PDF 相关操作

python 复制代码
# !/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: JHC000abc@gmail.com
@file: util_pdf.py
@time: 2025/3/28 15:22
@desc:提取pdf第一页图片 ,检查url中pdf是否完整

"""
import os
import time
import requests
from requests.exceptions import RequestException
from io import BytesIO
import PyPDF2
import pypdf
from pdf2image import convert_from_path
from uuid import uuid4


class PdfCheck:
    """

    """

    def __init__(self, file, tmp_file="./tmp"):
        self.file = file
        self.reader = None
        self.file_obj = None
        self.pages = 0
        self.tmp_file = tmp_file
        os.makedirs(self.tmp_file, exist_ok=True)
        self.first_name = f"{self.tmp_file}/{uuid4()}.png"
        print("首页图片:", self.first_name)

    def __enter__(self):
        self.file_obj = open(self.file, 'rb')
        self.reader = PyPDF2.PdfReader(self.file_obj)
        self.num_pages = len(self.reader.pages)
        print("pdf 页数:", self.num_pages)
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.file_obj:
            self.file_obj.close()

    def save_first_page(self):
        """

        :return:
        """
        images = convert_from_path(self.file, first_page=1, last_page=1, poppler_path=r"./poppler/Library/bin")
        if images:
            # 保存第一页的图像
            images[0].save(self.first_name, 'PNG')
            print(f"{self.file} 首页图片已保存至 {self.first_name}")
            return self.first_name
        else:
            print(f"无法提取{self.file}首页图片。")
            return None

    def save_page_to_image(self, save_path="images"):
        """

        :return:
        """
        os.makedirs(save_path, exist_ok=True)
        images = convert_from_path(self.file, poppler_path=r"../poppler/Library/bin")
        res = []
        if images:
            for ind, image in enumerate(images, 1):
                save_file = os.path.join(save_path, f"{ind}.png")
                image.save(save_file, 'PNG')
                print(f"{self.file} 图片 {ind} 已保存至 {save_file}")
                res.append(save_file)
            return res
        else:
            print(f"无法提取{self.file}首页图片。")
            return None
    
    def check_pdf_integrity_with_pypdf(self, url, timeout=30):
        """
        下载 PDF 文件内容到内存,并使用 pypdf 库尝试打开和读取页数,
        以检查文件结构是否完整。
        """

        try:
            # 步骤 2: 下载整个文件内容到内存
            # 注意:对于大文件,这会占用大量内存
            response = requests.get(url, timeout=timeout, verify=False)
            response.raise_for_status()

            # 检查 Content-Type (可选,但推荐)
            content_type = response.headers.get('Content-Type', '').split(';')[0].strip().lower()
            if 'application/pdf' not in content_type:
                print(f"【失败】Content-Type 错误: {content_type}。这不是一个 PDF。")
                return False

            # 步骤 3: 使用 BytesIO 将二进制数据传递给 pypdf
            pdf_bytes = BytesIO(response.content)

            # 步骤 4: 尝试用 pypdf 打开和读取
            reader = pypdf.PdfReader(pdf_bytes)

            # 尝试访问页数,如果文件损坏,这通常会抛出异常
            num_pages = len(reader.pages)

            print(f"【成功】pypdf 成功打开文件。文件结构完整,总页数: {num_pages}")
            return True

        except pypdf.errors.PdfReadError as e:
            print(f"【失败】PDF 文件结构损坏。pypdf 无法读取。错误信息: {e}")
        except requests.exceptions.HTTPError as e:
            print(f"【失败】HTTP 错误: 状态码 {e.response.status_code} ({e})")
        except RequestException as e:
            print(f"【失败】网络请求错误: {e}")
        except Exception as e:
            print(f"【失败】发生未知错误: {e}")

        return False


if __name__ == '__main__':
    start = time.time()
    with PdfCheck(
            r"D:\Desktop\10001231-6+中国平安保险(集团)股份有限公司2024年度可持续发展报告(A)+2025-03-20.pdf") as pdf:
        pdf.save_page_to_image()
        print(pdf.num_pages)
    end = time.time()
    dif = end - start
    print("dif", dif)
相关推荐
databook24 分钟前
Manim进阶:用背景图片让你的数学视频脱颖而出
python·动效
温轻舟1 小时前
Python自动办公工具01-Excel文件编辑器
开发语言·python·编辑器·excel·温轻舟
星星上的吴彦祖1 小时前
多模态感知驱动的人机交互决策研究综述
python·深度学习·计算机视觉·人机交互
爱笑的眼睛112 小时前
PyTorch Lightning:重新定义深度学习工程实践
java·人工智能·python·ai
0思必得02 小时前
[Web自动化] HTTP/HTTPS协议
前端·python·http·自动化·网络基础·web自动化
纵有疾風起2 小时前
C++——多态
开发语言·c++·经验分享·面试·开源
rgb2gray3 小时前
增强城市数据分析:多密度区域的自适应分区框架
大数据·python·机器学习·语言模型·数据挖掘·数据分析·llm
氵文大师3 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
程序员爱钓鱼3 小时前
用 Python 批量生成炫酷扫光 GIF 动效
后端·python·trae