Home > Back-end >  Get all PDF files name under same folder and save in excel according to PDF file name
Get all PDF files name under same folder and save in excel according to PDF file name

Time:07-01

I have PDF files in same folder. How to get all PDF file names and save as excel file according to PDF file name. This is what I have tried

def get_files(pdf_path):
    import os
    os.chdir(pdf_path)
    files = os.listdir()
    files = [x for x in files if x.endswith(".pdf")]
    return files 
    
files = get_files(pdf_path)

for i in files:
    save_as_excel(pdf_path, i)

CodePudding user response:

If you mean saving each filename as an empty excel file, try this :

import os
import openpyxl

pdf_path = '.'

def get_files(pdf_path):
    os.chdir(pdf_path)
    files = os.listdir()
    files = [x for x in files if x.endswith(".pdf")]
    return files 
    
files = get_files(pdf_path)

# create an empty workbook (excel file)
wb = openpyxl.workbook.Workbook()
for i in files:
    output_path = os.path.join(pdf_path, i).replace('.pdf', '.xlsx')
    # save as an excel file with filename
    wb.save(output_path)
    print(output_path)

CodePudding user response:

As discussed on enter image description here

Lets say you have following pdf files in the directory sample.pdf, sample2.pdf, sample3.pdf. The xlsx files will be created in the same folder with following filename sample.pdf.xlsx, sample2.pdf.xlsx, sample3.pdf.xlsx

Let me know if you have any doubts in the above code.

  • Related