如何解决如何以相同的表格格式从 pdf 中提取文本?
我正在使用 python 从 pdf 中提取数据。我正在使用 .extractText() 从 pdf 中获取我需要的数字。表中列出了一年中每个月的前两组数字(即,一月 = 203.25 和五月 = 167)。我遇到的问题是我需要将数字保持分开,以便我知道每个月的价值。每个月的数字可以有以下格式:1 到 9999 之间的值以及 0 到 2 个小数位之间的值。缺乏一致性使得尝试和使用蛮力方法变得困难。有没有办法可以将表格中每个单元格的值分开?
生活用水 = 203.25179.35188.83169.92167149.9144.61157.79157.23176.17185.46198.58 表格布局照片:1:https://i.stack.imgur.com/K4Wst.png
space_heating = 481.85371.63304.67166.866.720000157.21331.04494.27 表格布局 2 的照片:https://i.stack.imgur.com/3FkIV.png
这是我的代码
pdf_path = r'I 02-01.pdf'
import PyPDF2
pdf_file = open(pdf_path,'rb')
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page_content = ''
for i in range(0,number_of_pages):
page = read_pdf.getPage(i)
page_content = page_content+page.extractText()
page_content = page_content.encode('utf-8')
page_content = str(page_content)
#get start and end string position for DHW information
start_DHW = page_content.find("(64)m")+6
end_DHW = page_content.find("Output from water heater (annual)")
DHW = page_content[start_DHW:end_DHW]
print(DHW)
start_space_heating = page_content.find("(98)m")+6
end_space_heating = page_content.find("Total per year (kWh/year) = Sum(98)")
space_heating = page_content[start_space_heating:end_space_heating]
print(space_heating)
start_elec = page_content.find("Total electricity for the above,kWh/year")+73
end_elec = page_content.find("Energy for lighting (calculated in Appendix L)")
electrical = page_content[start_elec:end_elec]
print(electrical)
start_lighting = page_content.find("Energy for lighting (calculated in Appendix L)")+51
end_lighting = page_content.find("Total delivered energy for all uses (307) + (309) + (310) + (312) + (315) + (331) + (332)...(237b) =")
lighting = page_content[start_lighting:end_lighting]
print(lighting)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。