简体   繁体   中英

Run a python script on all files in a directory

First time posting a question here, hopefully, someone who experienced/tried this please share your insights... I've been working to get this far in the last few days and nights... now I am getting nowhere to loop this script on every file in a directory.

Bascially, these two scripts work perfectly fine it brings a pdf file and changes it to an excel workbook. Now what I need to do is going through all files from a selected directory and do the same job.


I am keep getting stuck at the opening the file stage - is this saying that the data (the pdf page - data[0]) cant be called in? or should i add more stages in to bring the dataset in...?

Do I have to create a list for the dataset so I can call in the data as you would have more than a data to call in.. is this why python can read the data[0] ???

在此处输入图片说明

Revised Script

# import 
import os
import glob
import pdftotext
import openpyxl
from pathlib import Path
from string import ascii_uppercase

# open a pdf file
def to_excel(pdf_file):
    with open(pdf_file,'rb') as f: 
        data = pdftotext.PDF(f)
        
# operate data to get titles, values 
datas = data[0].split('\r\n')

finalData = list()
for item in datas:
    if item != '':
        finalData.append(item)

finalDataRefined = list()
for item in finalData:
    if item != '                          BCA Scheduled Maintenance Questions' and item != ' Do you suspect there is Asbestos at the property?' and item != '    Yes' and item != '    No' and item != '\x0c':
        finalDataRefined.append(item.strip())

titles = list()
values = list()

for num, item in enumerate(finalDataRefined):
    if num % 2 == 0:
        titles.append(item)
    else:
        values.append(item)

# get an output file name
       
OPRAST = values[1]
filename = work_dir / f"{OPRAST}.xlxs"

# create an excel workbook
excel_file = openpyxl.Workbook()
excel_sheet = excel_file.active

excel_sheet.append([])

alphaList = list(ascii_uppercase)
for alphabet in alphaList:
    excel_sheet.column_dimensions[alphabet].width = 20

excel_sheet.append(titles)
excel_sheet.append(values)

# save the excel workbook
excel_file.save(filename)
excel_file.close

# run a python script every file in a directory
alphaList = list(ascii_uppercase)

work_dir = Path(r"C:\Users\Sunny Kim\Downloads\Do Forms")
for pdf_file in work_dir.glob("*.pdf"):
    to_excel(pdf_file)

I basically know what you want to do, but your code's indent is not so readable... especially it's python.

Your goal is to create a excel for each pdf file in you prefix dir? or aggregate all the pdf files together to a single excel file?

The follow coding is for the first goal.

Code logic.

  1. get all the pdf file
  2. loop over all the pdf file, for each:
    1. open pdf file
    2. some operation
    3. export to excel file

You full code maybe like this(just guess):

# ----------------import part-------------------
import os
import glob
import pdftotext
import openpyxl
from string import ascii_uppercase
from pathlib import Path

def to_excel(pdf_file):
    with open(pdf_file, 'rb') as f: # this open the pdf file
        data = pdftotext.PDF(f)
    # ---------------operate the data, get title and value-----------
    datas = data[0].split('\r\n')

    finalData = list()
    for item in datas:
        if item != '':
            finalData.append(item)

    finalDataRefined = list()
    for item in finalData:
        if item != '                          BCA Scheduled Maintenance Questions' and item != ' Do you suspect there is Asbestos at the property?' and item != '    Yes' and item != '    No' and item != '\x0c':
            finalDataRefined.append(item.strip())

    titles = list()
    values = list()
    for num, item in enumerate(finalDataRefined):
        if num % 2 == 0:
            titles.append(item)
        else:
            values.append(item)

    # ------------------get output file name---------------------
    OPRAST = values[1]
    filename = work_dir / f"{OPRAST}.xlxs"
    # ------------------create excel file sheet------------------
    excel_file = openpyxl.Workbook()
    excel_sheet = excel_file.active

    excel_sheet.append([])

    alphaList = list(ascii_uppercase)
    for alphabet in alphaList:
        excel_sheet.column_dimensions[alphabet].width = 20

    excel_sheet.append(titles)
    excel_sheet.append(values)
    # --------------------save----------------
    excel_file.save(filename)
    excel_file.close
# -------------------main program---------------
alphaList = list(ascii_uppercase)
work_dir = Path(r"C:\Users\Sunny Kim\Downloads\Do Forms")

for pdf_file in work_dir.glob("*.pdf"):
    to_excel(pdf_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM