Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

not getting print from python script #273

Open
badushaebrahim opened this issue Sep 29, 2022 · 4 comments
Open

not getting print from python script #273

badushaebrahim opened this issue Sep 29, 2022 · 4 comments
Labels

Comments

@badushaebrahim
Copy link

The Question:
trying to pass back the file path that is create dbut not recieving any
any ideas
Any relevant python/javascript code:
my server

var multer  = require('multer');
var fs  = require('fs');
var path = require('path');
var pdfUtil = require('pdf-to-text');
const generateUniqueId = require('generate-unique-id');
const {PythonShell} =require('python-shell');
// var options = {
//     root: 
// };

// pages up to
// var option = {from: 0, to: 10};
var defpath =path.join(__dirname)
var dir2 = '/home/badusha/Desktop/min/nodejs-simple-file-upload/uploads/';
// var dir3 = '/home/badusha/Desktop/min/nodejs-simple-file-upload/tmp/';
var full_story = "12"
var app = express();
app.set('view engine', 'ejs');

app.get('/', (req, res) => {
    res.render('index');
});

var storage = multer.diskStorage({
    destination: function (req, file, callback) {
        var dir = './uploads';
        if (!fs.existsSync(dir)){
            fs.mkdirSync(dir);
        }
        callback(null, dir);
    },
    filename: function (req, file, callback) {
        callback(null, file.originalname);
    }
   
});
var upload = multer({storage: storage}).array('files', 12);
app.post('/upload', function (req, res, next) {
    upload(req, res, function (err) {
        if (err) {
            return res.end("Something went wrong:(");
            // console.log(filename)
        }
        var filenames =dir2+"/"+req.files[0].filename
        console.log(filenames)
	var argz = defpath+"/uploads/"+req.files[0].filename
        let options = {
		mode: 'text',
		pythonOptions: ['-u'], // get print results in real-time
		//   scriptPath: 'path/to/my/scripts', //If you are having python_test.py script in same folder, then it's optional.
		args: [argz] //An argument which can be accessed in the script using sys.argv[1]
	    };
	    console.log("53")
	PythonShell.run('new.py', options, function (err, result){
		console.log("started py")
		if (err) throw err;
		// result is an array consisting of messages collected
		//during execution of script.
		console.log('result: ', result.toString());
		res.sendFile(argz+'0.mp3', options, function (err) {
			if (err) {
			    next(err);
			} else {
			    console.log('Sent:', fileName);
			}
		    });
	  });
	  
        

         
        //     res.send("done")
          
    
})

app.listen(3000);

# importing required modules 
import sys
import PyPDF2
from gtts import gTTS
import random
random.seed(5)
# creating a pdf file object 
pdfFileObj = open(sys.argv[1], 'rb') 
rap = sys.argv[1]   
# creating a pdf reader object 
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
    
# printing number of pages in pdf file 
k =0
txt=""
pgno = pdfReader.numPages
while(k< pgno):

# creating a page object 
	pageObj = pdfReader.getPage(k) 
	
	# extracting text from page 
	txt+=pageObj.extractText()
	k+=1
    
# closing the pdf file object 

pdfFileObj.close()
tts = gTTS(txt)
ran = random.random()*0
# nam =rap+str(ran)+'0.mp3'
nam =rap+'0.mp3'
tts.save(nam)

print(nam)

dependecy

    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "fs": "0.0.1-security",
    "generate-unique-id": "^2.0.1",
    "gtts": "^0.2.1",
    "multer": "^1.3.0",
    "pdf-to-text": "^0.0.7",
    "python-shell": "^3.0.1"
  },```
@badushaebrahim
Copy link
Author

hi
am new here so am trying to make an web app to convert pdf to audio file
so i got pdf file and wheen i try tor print file name its not coming out any idea

check out my whole project at
https://github.com/badushaebrahim/pdf-to-audio

@badushaebrahim
Copy link
Author

thanks

@Almenon
Copy link
Collaborator

Almenon commented Sep 30, 2022

Not sure, I tried running some code adapted from your example and it worked:

import sys
import PyPDF2
from gtts import gTTS
import random
random.seed(5)
# creating a pdf file object 
pdfFileObj = open(sys.argv[1], 'rb') 
rap = sys.argv[1]   
# creating a pdf reader object 
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
    
# printing number of pages in pdf file 
k =0
txt=""
pgno = pdfReader.numPages
while(k< pgno):

# creating a page object 
	pageObj = pdfReader.getPage(k) 
	
	# extracting text from page 
	txt+=pageObj.extractText()
	k+=1
    
# closing the pdf file object 

pdfFileObj.close()

tts = gTTS(txt)
ran = random.random()*0
nam =rap+str(ran)+'0.mp3'
nam =rap+'0.mp3'
tts.save(nam)

print(nam)
const {PythonShell} =require('python-shell');

# be sure to replace the below!
var argz = 'put your path here';
let options = {
    mode: 'text',
    pythonOptions: ['-u'], // get print results in real-time
    //   scriptPath: 'path/to/my/scripts', //If you are having python_test.py script in same folder, then it's optional.
    args: [argz] //An argument which can be accessed in the script using sys.argv[1]
};
console.log("53")
PythonShell.run('new.py', options, function (err, result){
    console.log("started py")
    if (err) throw err;
    // result is an array consisting of messages collected
    //during execution of script.
    console.log('result: ', result.toString());
});

As a rule of thumb when debugging, try to simplify your code until it's working, and then build back up to figure out where it went wrong.

@Qodestackr
Copy link

hi am new here so am trying to make an web app to convert pdf to audio file so i got pdf file and wheen i try tor print file name its not coming out any idea

check out my whole project at https://github.com/badushaebrahim/pdf-to-audio
Hello, fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants