Search code examples
pythonpython-3.xcsvpypdf

Data being written in a single line in some csv file


I have written some code to read data from a specific page of a "pdf" file and write it to a csv file using python. It does it's job only partially. However, when it comes to write data to a csv file, it writes those in a single line instead of the regular pattern. How should I modify my script to serve the purpose? Thanks in advance.

Here is what I've tried so far:

import csv
from PyPDF2 import PdfFileReader

outfile = open("conversion.csv",'w', newline='')
writer = csv.writer(outfile)

infile = open('some.pdf', 'rb')
reader = PdfFileReader(infile)
contents = reader.getPage(7).extractText().split('\n')
writer.writerow(contents)

print(contents)
infile.close()

Data in pdf are like these:

Creating a PivotTable Report 162
PivotCaches 165
PivotTables Collection 165
PivotFields 166
CalculatedFields 170

I'm getting data in csv output like:

Creating a PivotTable Report 162 PivotCaches 165 PivotTables Collection 165 PivotFields 166 CalculatedFields 170

Solution

  • For this Specific Code :

    as contents is a list of items[lines]

    contents = reader.getPage(7).extractText().split('\n')
    for each in contents:
        writer.writerow(each)
    
    print(contents)
    

    Try this and let me know.