【问题标题】:Convert PDF to XLS将 PDF 转换为 XLS
【发布时间】:2021-10-20 11:41:00
【问题描述】:

我想将 PDF 文件转换为 CSV 或 XLS。 我尝试通过使用 python tabula 来做到这一点:

#!/bin/bash
#!/usr/bin/env python3
import tabula

# Read pdf into list of DataFrame
df = tabula.read_pdf("File1.pdf", pages='all')

# convert PDF into CSV file
tabula.convert_into("File1.pdf", "File1.csv", output_format="csv", pages='all')

# convert all PDFs in a directory
#tabula.convert_into_by_batch("input_directory", output_format='csv', pages='all')

虽然python脚本将PDF转换为CSV,但十进制不正确。

例如 1.25 仅显示为 1.2。

所以我想将小数位增加到两位,以便在转换后的 CSV 文件中得到正确的数字。

有人可以帮我吗?

谢谢。

【问题讨论】:

  • 需要探索区域和列参数。像这样df = tabula.read_pdf(pdf_file, pages='all',area=(0, 8, 800, 840),columns=[91,269,380,470,520,580,657]。同样,它取决于pdf。如果你可以分享 PDF,那么我们可以看看。
  • 我的 PDF 文件中有 12 列。文件的前四行是标题。
  • 现在我可以得到小数点到 2 但 csv 文件的格式很奇怪。
  • 现在我可以将小数点设为 2,但 csv 文件的格式很奇怪。 PDF 文件有 2 页。第一页有第 3 行包含标题,第 4 行是 12 列的相应标题。第二页没有任何列标题。转换后的 CSV 显示 column1、column2 3 4 5 6 7 8 9、column10、column11、column12。而第二页像 column1, column2 一样正确显示。 . .第 12 列。在第一页中,第 2 到第 9 列显示为单列。请建议我该如何纠正这个问题?
  • 请分享 PDF 并更新您目前拥有的代码。

标签: python pdf python-3.7 pdftotext tabula


【解决方案1】:

根据需要,我们需要调整tabula 上的参数,以便数据导入有意义。我在 cmets 中建议的参数只是一个示例。要获得从 x 轴开始的列,我们需要使用 acrobat 的付费版本或使用一些轨迹。

所以代码会是这样的

导入和设置

import tabula
import pandas as pd
pdf_file='file1.pdf'
column_names=['Product','Batch No','Machin No','Time','Date','Drum/Bag No','Tare Wt.kg','Gross Wt.kg',
              'Net Wt.kg','Blender','Remarks','Operator']
df_results=[] # store results in a list

由于页面格式不同,我们需要分别处理。还有一些清理,比如删除不需要的列或特定值后的数据(参见第 2 页处理)

# Page 1 processing
try:
    df1 = tabula.read_pdf(pdf_file, pages=1,area=(95,20, 800, 840),columns=[93,180,220,252,310,315,333,367,
                                                                          410,450,480,520]
                         ,pandas_options={'header': None}) #(top,left,bottom,right)
    df1[0]=df1[0].drop(columns=5)
    df1[0].columns=column_names
    df_results.append(df1[0])
    df1[0].head(2)
    
except Exception as e:
    print(f"Exception page not found {e}")
# Page 2 processing
try:
    df2 = tabula.read_pdf(pdf_file, pages=3,area=(10,20, 800, 840),columns=[93,180,220,252,310,315,330,370,
                                                                          410,450,480,520]
                         ,pandas_options={'header': None}) #(top,left,bottom,right)

    row_with_Sta = df2[0][df2[0][0] == 'Sta'].index.tolist()[0]
    df2[0] = df2[0].iloc[:row_with_Sta]
    df2[0]=df2[0].drop(columns=5)
    df2[0].columns=column_names
    df_results.append(df2[0])
    df2[0].head(2)
except Exception as e:
    print(f"Exception page not found {e}")
#result = pd.concat([df1[0],df2[0]]) # concate both the pages and then write to CSV
result = pd.concat(df_results) # concate list of pages and then write to CSV
result.to_csv("result.csv")

请测试代码,因为我只有一定程度的验证 :)

【讨论】:

  • 是的,现在最终的 csv 看起来不错,但是它仍然需要针对以下几点进行调整:A) 我想保留 PDF 文件在 result.csv 中的标题的原始外观 B)需要删除序列号列 C)如果数字是 9.30,它显示为 9.3,我希望它显示为 2 个十进制数字 9.30 这样我们如何继续前进
  • 我创建了 bash shell 脚本来将所有这些放在一起,这样当目录中有 PDf 文件时,它应该将其转换为 csv sed -e "s/Input_PDF_FileName/$pdffilename/" -e "s/Output_CSV_FileName/$filename/g" -e "s/final_outputfile/$filename/" /root/scripts/pdf2xls/python_pdf2xls.py /usr/bin/python3 /root/scripts/pdf2xls/python_pdf2xls.py
  • 回溯(最近一次调用最后):文件“/root/scripts/pdf2xls/python_pdf2xls.py”,第 14 行,在 中,pandas_options={'header': None}) #(上、左、下、右)文件“/usr/local/lib/python3.7/dist-packages/tabula/io.py”,第 314 行,在 read_pdf 中引发 FileNotFoundError(errno.ENOENT, os.strerror(errno. ENOENT), path) FileNotFoundError: [Errno 2] No such file or directory: '/pdf2xls/Input_PDF_FileName'
  • 为什么不接受python脚本中替换的文件路径
  • 请将此作为新问题发布。
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 2010-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多