0

So, I am trying to convert multiple PDFs into CSV. I have a code that does the scanning and transforms everything into a single CSV as of now. However, instead of converting the data into one single CSV, I want Python to save individual CSVs for each PDF processed (this is primarily to identify a new file as there is more code on top of this that formats the data). I have the below code now, but I can't find a way to save multiple files. Any assistance will be appreciated.

import os
import pdfplumber
import pandas as pd

directory = r'Folfer Path'

lines = []

for filename in os.listdir(directory):
    if filename.endswith(".pdf"):
        pdf = os.path.join(directory, filename)


        with pdfplumber.open(pdf) as pdf:
            pages = pdf.pages
            for page in pdf.pages:
                text = page.extract_text()
                for line in text.split('\n'):
                    lines.append(line)
                    print(line)

            df = pd.DataFrame(lines)

df.to_csv('Folder Path/ filename.csv')
0

1 Answer 1

1
directory = r'pdfs'
csv_dir = r"csvs"
lines = []
for filename in os.listdir(directory):
    if filename.endswith(".pdf"):
        pdf_path = os.path.join(directory, filename)
        with pdfplumber.open(pdf_path) as pdf_file:
            for page in pdf_file.pages:
                text = page.extract_text()
                for line in text.split('\n'):
                    lines.append(line)
                df = pd.DataFrame(lines)
            df.to_csv(os.path.join(csv_dir, filename[0:-4] + ".csv"))

That might an answer to your problem. You have some indentation problems and non-dynamic file names. In this code, the CSV files will be saved to a folder 'csvFiles', make sure you create it before running the code. it should be looking like that: Path...

  • path..
  • your python script file (.py)
  • pdfs(Folder)
    • pdf1.pdf
    • pdf2.pdf
    • ...
  • csvs(Folder)
    • csv1.csv
    • csv2.csv
    • ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.