-1

I want to convert some PDF file to TXT file in a bash script.

pdf2txt.py -o otuput.txt input.pdf

this is the command to do the task for a single file. But for large set of file I am trying to do the following.

#!/bin/bash
cd /home/z..../P...../file/pdf

python << END

import os
file_lst = os.listdir(r'/home/z..../P...../file/pdf')

out_file_lst = []
l = len(file_lst)

for i in file_lst:
    out_file_lst.append(file_lst[0].split('.')[0] + '.txt')

for i in range(l):
    pdf2txt.py -o out_file_lst[i] file_lst[i] 
    # How to run this bash command inside of python ?

END
4
  • This has been answered previously. See, for example, stackoverflow.com/questions/4256107/… or stackoverflow.com/questions/20415522/… Commented Oct 7, 2014 at 14:57
  • Thanks for your suggestion but that is not I am looking for. Commented Oct 7, 2014 at 16:17
  • 1
    Why are you making this a bash script in the first place? You have a Python script, and the only bash command (cd ...) can be moved into the Python script anyway using os.chdir. Alternately, make the whole thing a bash script without using Python. Alternating languages like this serves no purpose. Commented Oct 7, 2014 at 16:52
  • @Md.ZakirHossan - you are specifically asking how to run a bash command from python, and stackoverflow.com/questions/4256107/… and stackoverflow.com/questions/20415522/… show you exactly how to do it - all you need to do is plugin your specific command. Commented Oct 7, 2014 at 17:02

2 Answers 2

3

You can create a bin/bash script that convert all pdf files in your directory in txt files.

#!/bin/bash
for file in *.pdf;
do pdftotext "$file" "$file.txt";
done
Sign up to request clarification or add additional context in comments.

1 Comment

except output ends up as somefile.pdf.txt.
0

I strongly suggest subprocess lib.

For example:

return_code = subprocess.call(['ls', '-l'])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.