文本内容的pdf到csv文件如何转换?

2024-04-24 23:37:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要一个PDF文件作为输入。作为一个输出文件,我想要一个csv文件来显示。所以pdf文件中的所有文本数据都应该转换成csv文件。但我不明白这是怎么发生的…我需要你的帮助,最早,因为我已经尝试做了,但做不到。你知道吗

我所做的是使用一个名为tabla py的库将pdf转换成csv文件。它确实创建了csv格式,但是没有内容从pdf文件复制到csv文件。你知道吗

这是密码

from tabula import convert_into,read_pdf
import tabula
df = tabula.read_pdf("crimestory.pdf", spreadsheet=True, 
                     pages='all',output_format="csv")
df.to_csv('crimestoryy.csv', index=False)

输出应该是一个csv文件,其中包含数据。 我得到的是一个空白的csv文件。你知道吗


Tags: 文件csv数据py文本import密码内容
2条回答

我自己找到了这个问题的答案 为了解决这个问题,我提出了将pdf文件转换成文本文件的方法。然后我把这个文本文件转换成csv文件文件。在这里这是我的密码。你知道吗

你知道吗转换.py你知道吗

import os.path
import csv
import pdftotext
#Load your PDF
with open("crimestory.pdf", "rb") as f:
   pdf = pdftotext.PDF(f)

# Save all text to a txt file.
with open('crimestory.txt', 'w') as f:
    f.write("\n\n".join(pdf))

save_path = "/home/mayureshk/PycharmProjects/NLP/"

completeName_in = os.path.join(save_path, 'crimestory' + '.txt')
completeName_out = os.path.join(save_path, 'crimestoryycsv' + '.csv')

file1 = open(completeName_in)
In_text = csv.reader(file1, delimiter=',')

file2 = open(completeName_out, 'w')
out_csv = csv.writer(file2)

file3 = out_csv.writerows(In_text)

file1.close()
file2.close()

试试这个,希望有用

import tabula

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

或者

df = tabula.read_pdf("crimestory.pdf", encoding='utf-8', spreadsheet=True, pages='all')
df.to_csv('crimestory.csv', encoding='utf-8')

或者

from tabula import read_pdf
df = read_pdf("crimestory.pdf")
df
#make sure df displays your pdf contents in the output

from tabula import convert_into
convert_into("crimestory.pdf", "crimestory.csv", output_format="csv")
!cat.crimestory.csv

相关问题 更多 >