首页 > 解决方案 > 引用原始脚本路径的 Cx_freeze 独立文件

问题描述

伙计们,我真的需要你们的帮助,我要出去度假,但我无法设置我仍然每天使用的脚本在另一台机器上运行。

在我的代码中,我使用 pdf2image 转换器来检查是否正确填充了某些文件。我踩到了这个错误,无法通过,希望你能帮忙。

这是我想要独立的代码的一部分

import PyPDF2
import openpyxl
from datetime import datetime, timedelta
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.common.exceptions import NoSuchElementException as NSEE
from selenium.common.exceptions import TimeoutException as TimeExpt
from selenium.common.exceptions import NoAlertPresentException as NAPE
from selenium.common.exceptions import StaleElementReferenceException as SERE
from selenium.common.exceptions import ElementNotInteractableException as ENIE
import shutil
from tkinter.filedialog import askopenfilename, askdirectory
from PIL import Image 
import pytesseract 
import sys 
from pdf2image import convert_from_path

def verificar_Guias(docs_encontrados, NrID, valorVerif):
        for z in range(0,len(docs_encontrados)):
                if 'NOTA' not in docs_encontrados[z].upper():
                        PDF_fileGuia = docs_encontrados[z]
                elif 'NOTA' in docs_encontrados[z].upper():
                        PDFfileNota = docs_encontrados[z]

        
        pages = convert_from_path(PDF_fileGuia, 200, output_folder = path_robo + '\\OCR', poppler_path= path_robo+ '\\poppler-20.12.1\\Library\\bin' ,output_file='nome do arquivo')
        pytesseract.pytesseract.tesseract_cmd = path_robo + '\\Tesseract-OCR\\tesseract.exe'
        image_counter = 1
        for page in pages:
                filenameGuia = path_robo+ "\\OCR\\page_"+str(image_counter)+".jpg"
                page.save(filenameGuia, 'JPEG')
                image_counter = image_counter + 1
                outfile = path_robo + '\\OCR\\' + PDF_fileGuia.replace('pdf','txt')
                f = open(outfile, "a")
                text = str(((pytesseract.image_to_string(Image.open(filenameGuia)))))
                if text.find(valorVerif) > 1:
                        print('Ok')
                        valorGuia = text[text.find(valorVerif):text.find(valorVerif)+len(valorVerif)]
                        break
                else:
                        if text.find('FEDTJ')> 0 or text.find('FEDTI')> 0:
                                valorGuia = text[text.find('Valor Total ')+12:text.find(',',text.find('Valor Total ')+12)+3]
                        else:
                                valorGuia = text[text.find('valor: R$ ')+10:text.find(',',text.find('valor: R$ ')+10)+3]

我的 cx_freeze 设置:

from cx_Freeze import setup, Executable

if sys.platform == "win32":
    base = "Win32GUI"



executables = [Executable("Bot - Com OCR funcional 2.py", base=base)]
includefiles = [os.path.dirname(pdf2image.__file__),'chromedriver.exe','Leia-me.txt',r'C:\Users\renat\OneDrive\Área de Trabalho\Scripts\Funcionando\ok\Inserir OBP\Tesseract-OCR',r'C:\Users\renat\OneDrive\Área de Trabalho\Scripts\Funcionando\ok\Inserir OBP\poppler-20.11.0']
#excludes = ['pandas','matplotlib','numpy','sqlite3','scipy']
packages = ["os", "time", "PyPDF2", "openpyxl" , "datetime" , "tkinter","selenium",  "sys", "pytesseract","PIL","pdf2image"]
options = {
    'build_exe': {    
        'packages':packages,
        'include_files':includefiles,
        #'excludes':excludes,
        
    },    
}

setup(
    name = "Inserção de Ocorrências",
    options = options,
    version = "1.0",
    description = 'Robô de inserção de ocorrências',
    executables = executables

错误消息(在另一台电脑上运行时)是:

DevTools listening on ws://127.0.0.1:49743/devtools/browser/ee6228c9-e07e-4c22-9087-10bc6c4d9046
C:/Users/Renato/Desktop/Renato - 12-03-2021.xlsx
156572020--8
Traceback (most recent call last):
  File "C:\Users\renat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pdf2image\pdf2image.py", line 441, in pdfinfo_from_path
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 858, in __init__
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2288.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1311, in _execute_child
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\renat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\cx_Freeze\initscripts\__startup__.py", line 41, in run
  File "C:\Users\renat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\cx_Freeze\initscripts\Console.py", line 36, in run
  File "Bot - Com OCR funcional 2.py", line 237, in <module>
  File "Bot - Com OCR funcional 2.py", line 59, in verificar_Guias
  File "C:\Users\renat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pdf2image\pdf2image.py", line 97, in convert_from_path
  File "C:\Users\renat\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pdf2image\pdf2image.py", line 467, in pdfinfo_from_path
pdf2image.exceptions.PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?

如果有人可以帮助我,我会很受欢迎,因为这是唯一让我远离我已经推迟的假期的事情

标签: pythonpython-3.xcx-freeze

解决方案


推荐阅读