PDF到CSV -转换的CSV已互换列的内容

o4tp2gmn  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(101)

我正在尝试使用Python和下面的代码将PDF文件转换为CSV。以前它是工作的;但是,最近它不工作。我在转换的CSV文件中获得互换的列内容。
引导我在我的代码中修复此列问题。我只关心PDF转换的第一页,因为我需要删除表的第一行。

#!/usr/bin/env python3
import tabula
import pandas as pd
import csv

pdf_file='/pdf2xls/Input.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']

# Page 1 processing
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
#df1[0].head(2)

#df1[0].to_csv('result.csv')

result = pd.DataFrame(df1[0]) # concate both the pages and then write to CSV
result.to_csv("/pdf2xls/Input.csv")

字符串

sqxo8psd

sqxo8psd1#

假设你的pdf总是至少有两页,最后一页有页脚,你可以试试:

# pip install pdfplumber
import pdfplumber
import pandas as pd

pdf = pdfplumber.open("23JJ0WL139.pdf")

tables = []
for p in pdf.pages:
    ta = p.extract_tables()[0]
    if str(p) == "<Page:1>":
        header = ta[4]
        tables.append(pd.DataFrame(ta[5:]))
    else:
        tables.append(pd.DataFrame(ta))
                    
df = pd.concat(tables).iloc[:-3].set_axis(header, axis=1)

字符串
输出量:

print(df)

   Product    Batch No Machin\nNo  ... Net\nWt.kg Blender Operator
0    GC950  23JJ0WL139     WB_101  ...      51.40            Anand
1    GC950  23JJ0WL139     WB_101  ...      51.60            Anand
2    GC950  23JJ0WL139     WB_101  ...      51.20            Anand
3    GC950  23JJ0WL139     WB_101  ...      51.20            Anand
4    GC950  23JJ0WL139     WB_101  ...      51.80            Anand
..     ...         ...        ...  ...        ...     ...      ...
11   GC950  23JJ0WL139     WB_101  ...      51.60            RAHUL
12   GC950  23JJ0WL139     WB_101  ...      51.60            RAHUL
13   GC950  23JJ0WL139     WB_101  ...      51.80            RAHUL
14   GC950  23JJ0WL139     WB_101  ...      51.40            RAHUL
15   GC950  23JJ0WL139     WB_101  ...      51.80            RAHUL

[140 rows x 11 columns]

2q5ifsrm

2q5ifsrm2#

代码的问题似乎与转换后的CSV文件中的列交换有关。此问题可能是由使用tabula.read_pdf时指定列的方式引起的。您可以修改列坐标以确保正确识别和提取每个列。

#!/usr/bin/env python3
import tabula
import pandas as pd

pdf_file = '/pdf2xls/Input.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']

# Page 1 processing
df1 = tabula.read_pdf(pdf_file, pages=1, area=(95, 20, 800, 840), columns=[93, 180, 220, 252, 310, 355, 380, 410,
                                                                     450, 480, 520], pandas_options={'header': None})

df1[0].columns = column_names
df1[0] = df1[0].drop(0)  # Remove the first row
result = pd.DataFrame(df1[0])
result.to_csv("/pdf2xls/Input.csv", index=False)  # Set index=False to avoid writing the index column

字符串
在此修改后的代码中:
1.我调整了columns参数中的列坐标,以确保正确识别每一列。
1.我已经使用df1[0] = df1[0].drop(0)删除了第一行,以摆脱标题行。
1.在将DataFrame保存到CSV时,我添加了index=False,以防止将索引作为单独的列写入。

相关问题