BookmarkSubscribeRSS Feed
awkward-map
Calcite | Level 5

I am working in SAS Viya because I am needing to use proc python. I thought that it would be best to do this in SAS Viya since the upper part of my code grabs various PDFs from a website. Then, I am trying to get the proc python to run the translation. 

 

This is the code I am using: 

 

/*start code*/ 

%let output_folder =C:\Users\xxx; 

proc python;

submit;
#Python code to extract text from PDFs and translate
import os
from polyglot.text import Text
from googletrans import Translator
import fitz  

pdf_folder = r'&output_folder.'
translator = Translator()

for pdf_file in os.listdir(pdf_folder):
    if pdf_file.lower().endswith('.pdf'):
        doc = fitz.open(os.path.join(pdf_folder, pdf_file))
        full_text = ""
        for page in doc:
            full_text += page.get_text()
        # Detect language and translate if not English
        text_obj = Text(full_text)
        lang = text_obj.language.code
        if lang != 'en':
            translated = translator.translate(full_text, dest='en')
            print(f"Translated {pdf_file}:")
            print(translated.text)
endsubmit;
run;
/*End code*/ 
 
When I run this code I get the following error:  ERROR: Unhandled Python exception.
Does SAS Viya not have certain packages installed? Python is initiated per the log. 
5 REPLIES 5
Tom
Super User Tom
Super User

You appear to define a SAS macro variable.

%let output_folder =C:\Users\xxx; 

Then later in the middle of your PYTHON code you appear to be trying to reference the macro variable's value.

pdf_folder = r'&output_folder.'

Normally the SAS macro processor will ignore macro triggers (such as & and %) when they are enclosed in single quotes.

 

What happens when you instead hardcode the filename in the PYTHON code?

 

Also I seriously doubt that VIYA or Python will be able to use WINDOWS/DOS style filenames like that.

awkward-map
Calcite | Level 5

This seems to have helped but when I run it, I still get an error and now it adds, 

ModuleNotFoundError: No module named 'polyglot'
 
When I run pip install polyglot (as I see this is a typical way to install packages on python) it says it doesn't recognize install. Is that because in python you would install the package on the shell and not the code area? 
Tom
Super User Tom
Super User

It should be possible to install packages, but you need to do it in advance.

Check out this thread:

https://communities.sas.com/t5/Administration-and-Deployment/How-do-I-Install-python-packages-on-SAS...

 

awkward-map
Calcite | Level 5

I am using SAS Viya on the web...I don't understand how I am to do what the link says. Sidenote, I have also tried using python directly but apparently there is an issue with downloading polyglot with the current version of pip...

Tom
Super User Tom
Super User

Send the question to whoever is running the VIYA server that you are using.  If that is SAS itself then open a support ticket with SAS and ask them if they can install the python package you need.