0

I have started looking into functions so that i could create an app with gui and a few buttons that executes different scripts.

Currently i am using pysimplegui for the creation of a simple gui and files chooser in first script. File chosen resides as value[0] and i would like to pass the value to a function defined in second script and then run the function in first script.

i have tried from ocr_my_pdf import ocr_my_pdf but it comes with a circular import error

Any help is appreciated, thanks!

first.py

sg.theme('Reddit')  # Add a touch of color
# All the stuff inside your window.
layout = [[sg.Text('Choose a file / files:'), sg.Input(), sg.FilesBrowse()],
          [sg.Button('Split', key='split', enable_events=True), sg.Button('OCR', key='ocr', enable_events=True),
           sg.Button('Cancel/Close')],
          [sg.Text('Progress: ')],
          [sg.MLine(key='-ML-' + sg.WRITE_ONLY_KEY, size=(40, 5), reroute_stdout=True, auto_refresh=True)]]

# Create the Window
window = sg.Window('I am a title', layout).Finalize()

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read(timeout=50)

    if event == "split":
        pdf_file = values[0]
        window['-ML-' + sg.WRITE_ONLY_KEY].print('Starting...')
        threading.Thread(target=split_pdf, daemon=True).start()

    if event == "ocr":
        pdf_file = values[0].split(';')

        window['-ML-' + sg.WRITE_ONLY_KEY].print('Starting...')
        threading.Thread(target=ocr_my_pdf, daemon=True).start()

    if event == sg.WIN_CLOSED or event == 'Cancel/Close':
        break

window.close()
second.py
from ocrmypdf import ocr

def ocr_my_pdf():
    pdf_file = values[0].split(';')
    # # pdf_files = pdf_file.split(';')
    #
    for pdfs in pdf_file:
        pdf_path = pathlib.Path(pdfs).parent
        pdf_name = pathlib.Path(pdfs).stem
        if pdfs.endswith('.pdf'):
            if __name__ == '__main__':
                ocr(pdfs, output_file=f'{pdf_path}/OCR_{pdf_name}.pdf',
                    sidecar=f'{pdf_path}/OCR_{pdf_name}.txt', rotate_pages=True, deskew=True, jobs=2, use_threads=True, optimize=1,
                    fast_web_view=0, output_type='pdf')

running this results in

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\cheeh\Desktop\PyCharmPortable\PycharmProjects\BSA projects\ocr_my_pdf_f.py", line 47, in ocr_my_pdf
    pdf_file = values[0].split(';')
NameError: name 'values' is not defined
coconutxyz
  • 15
  • 4

0 Answers0