2401_84482571 2024-06-06 10:42 采纳率: 66.7%
浏览 103
已结题

使用pdfplumber运行读取pdf,结果为空

img


是因为我的代码有问题吗,但是运行其他的都没有问题,一到提取内容就出现了问题,是为什么呢

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-06 10:42
    关注

    引自免费微信小程序:皆我百晓生

    在您提供的代码中,看起来没有明显的语法错误。不过,有时候PDF文件可能由于加密、格式问题或者其他原因导致文本无法被正确提取。这里有几个步骤可以帮助您排查问题。

    1. 检查PDF文件:确保您的PDF文件不是图片格式,而是包含可提取的文本。您可以尝试打开PDF文件并查看其内容是否可以复制粘贴,如果不能,那可能是文件本身的问题。

    2. 更新pdfplumber:确保您的pdfplumber版本是最新的。有时旧版本可能无法处理某些PDF格式。您可以使用以下命令更新pdfplumber

      pip install --upgrade pdfplumber
      
    3. 使用其他工具验证:尝试使用其他Python库(如PyPDF2或PDFMiner)来提取文本,看看是否有不同的结果。这有助于确定问题是否出在pdfplumber上。

    4. 尝试不同方法提取pdfplumber提供多种方式提取文本,例如通过页面区域、行或单词。您可以试试这些方法看是否能成功提取:

      import pdfplumber
      
      def extract_text_with_areas(pdf_path):
          with pdfplumber.open(pdf_path) as pdf:
              for page in pdf.pages:
                  text = ""
                  for area in page.extract_areas():
                      text += area["text"] + "\n"
                  return text
      
      def extract_text_with_lines(pdf_path):
          with pdfplumber.open(pdf_path) as pdf:
              for page in pdf.pages:
                  text = ""
                  for line in page.extract_text().split("\n"):
                      text += line + "\n"
                  return text
      
      def extract_text_with_words(pdf_path):
          with pdfplumber.open(pdf_path) as pdf:
              for page in pdf.pages:
                  text = ""
                  for word in page.extract_words():
                      text += word["text"] + " "
                  return text
      
      # 使用上述函数之一替换原代码中的extract_text()
      text = extract_text_with_areas("测试.pdf")
      print(text)
      
    5. 解密PDF:如果PDF文件被加密,您可能需要提供密码才能访问其内容。使用pycryptodome库来尝试解密:

      首先安装库:

      pip install pycryptodome
      

      然后添加解密部分到您的代码中:

      from Crypto.Cipher import AES
      from Crypto.Util.Padding import pad, unpad
      
      def decrypt_pdf(ciphertext, key, iv):
          cipher = AES.new(key, AES.MODE_CBC, iv)
          plaintext = unpad(cipher.decrypt(ciphertext), AES.block_size)
          return plaintext.decode()
      
      def extract_text_decrypted(pdf_path, password):
          with open(pdf_path, 'rb') as f:
              ciphertext = f.read()
      
          # 假设密码为"my_password",根据实际情况调整
          key = b'my_password' * (16 // len(b'my_password'))
          iv = ciphertext[:16]
      
          decrypted_text = decrypt_pdf(ciphertext, key, iv)
      
          # 将解密后的文本写入新文件,然后尝试提取
          with open('decrypted_text.txt', 'w') as decrypted_file:
              decrypted_file.write(decrypted_text)
      
          # 使用之前的方法从新文件提取文本
          text = extract_text_with_areas('decrypted_text.txt')
          return text
      
      # 替换原代码中的提取部分
      text = extract_text_decrypted("测试.pdf", "my_password")
      print(text)
      

    如果以上所有方法都无法解决您的问题,那么问题可能出在PDF文件本身,您可能需要寻求专业的PDF修复服务或与文件创建者确认是否存在格式问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 6月14日
  • 已采纳回答 6月6日
  • 创建了问题 6月6日