问题描述
我正在为项目编译一些数据,并且一直在使用PyPDF4从其源PDF文件读取此数据,但是对于某些字符无法正确显示我一直遇到麻烦。这是我的代码:
from PyPDF4 import PdfFileReader
import pandas as pd
import numpy as np
import os
import xml.etree.cElementTree as ET
# File name
pdf_path = "PV-9-2020-10-23-RCV_FR.pdf"
# Results storage
results = {}
# Start page
page = 5
# Lambda to assign Votes
serify = lambda Voters,Vote: pd.Series({Voter.strip(): Vote for Voter in Voters})
with open(pdf_path,'rb') as f:
# Get PDF reader for PDF file f
pdf = PdfFileReader(f)
while page < pdf.numPages:
# Get text of page in PDF
text = pdf.getPage(page).extractText()
proposal = text.split("\n+\n")[0].split("\n")[3]
# Collect all pages relevant pages
while text.find("\n0\n") is -1:
page += 1
text += "\n".join(pdf.getPage(page).extractText().split("\n")[3:])
# Remove corrections
text,corrections = text.split("CORRECCIOnes")
# Grab relevant text !!! This is where the missing characters show up.
text = "\n,".join([n[:n.rindex("\n")] for n in text.split("\n:")])
for_list = "".join(text[text.index("\n+\n")+3:text.index("\n-\n")].split("\n")[:-1]).split(",")
nay_list = "".join(text[text.index("\n-\n")+3:text.index("\n0\n")].split("\n")[:-1]).split(",")
abs_list = "".join(text[text.index("\n0\n")+3:].split("\n")[:-1]).split(",")
# Store data in results
results.update({proposal: dict(pd.concat([serify(for_list,1),serify(nay_list,-1),serify(abs_list,0)]).items())})
page += 1
print(page)
results = pd.DataFrame(results)
我遇到困难的字符没有出现在使用extractText
提取的文本中。例如,Ždanoka
变成"danoka
,Štefanec
变成-tefanc
。似乎大多数字符都是东欧的,这让我觉得我需要拉丁解码器之一。
我已经查看了PyPDF4的一些功能,似乎它具有许多相关的编解码器,包括latin1
。我尝试使用PyPDF4.generic.codecs
模块中不同的功能对文件进行解码,或者字符不能仍然显示,或者代码在无法识别的字节上抛出错误。
我还没有尝试在同一文件的不同字节上使用多个编解码器,这似乎需要一些时间。我在代码中缺少可以轻松解决此问题的内容吗?还是更有可能我 必须使用PyPDF4的功能定制适合的解决方案?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)