gpt4 book ai didi

Interact with an exe file which is converted from py file(与从py文件转换而来的exe文件交互)

转载 作者:bug小助手 更新时间:2023-10-25 17:45:06 27 4
gpt4 key购买 nike



I have the following python script. In my python script i run it to convert some pdfs to excel. When i run it i enter the filename which the folders with the pdfs exist and i press enter and my script proceed the pdf files and return me a structured excel file.

我有以下的Python脚本。在我的python脚本中,我运行它来将一些pdf转换为EXCEL。当我运行它时,我输入带有pdf的文件夹所在的文件名,然后按Enter,我的脚本继续处理pdf文件,并返回给我一个结构化的Excel文件。


I tried to convert the py file to an exe file using the following commands

我尝试使用以下命令将py文件转换为exe文件


pip install pyinstaller 
pyinstaller --onefile myscript.py

When i execute this i am getting back an exe file but when i run it a cmd pop up window open and close very quickly without giving me the opportunity to enter the folder name.
Can anyone help on that ?

当我执行此命令时,我得到的是一个可执行文件,但当我运行它时,一个cmd弹出窗口很快就会打开和关闭,但我没有机会输入文件夹名。有人能帮上忙吗?


This is the python script.

这是一个python脚本。


import pandas as pd
import re
import os
from datetime import date
import datetime
import tkinter as tk
from tkinter import simpledialog

def get_folder_name():
root = tk.Tk()
root.withdraw() # Hide the main window
folder = simpledialog.askstring("Folder Name", "Enter the folder name (e.g., 23.08.2023):")
return folder



def process_pdfs_and_generate_excel(base_directory, folder):
dfs = []
folder_path = os.path.join(base_directory, folder)
if not os.path.exists(folder_path):
print(f"Folder '{folder}' does not exist.")
return None

for root, _, files in os.walk(folder_path):
for filename in files:
if filename.endswith('.txt'):
filepath = os.path.join(root, filename)
with open(filepath, 'r') as file:
lines = file.readlines()

start_line = 18
weighted_average_line_index = None
for i, line in enumerate(lines[start_line:], start=start_line):
if "WEIGHTED AVERAGE" in line:
weighted_average_line_index = i
break

if weighted_average_line_index is not None:
table_data = [line.strip().split('|') for line in lines[start_line:weighted_average_line_index] if '|' in line]

column_names = ['ARRIVAL_DATE', 'CONTAINER_NO', 'GR_QTY', 'LOT_NUMBER', 'OTHER_PAPERS', 'MOISTURE', 'PROHIBITIVE', 'File Name', 'Folder Name', 'EXCEL_FILE_ID']
mapped_data = {}
for column_name, index in zip(column_names, [2, 3, 5, 6, 7, 8, 9, None, None, None]):
mapped_data[column_name] = []
for row in table_data:
if column_name == 'Folder Name':
mapped_data[column_name].append(folder)
elif column_name == 'File Name':
mapped_data[column_name].append(filename)
elif column_name == 'EXCEL_FILE_ID':
mapped_data[column_name].append(f'PT_EKAMAS_{folder}')
elif len(row) > index:
mapped_data[column_name].append(row[index].strip())
else:
mapped_data[column_name].append('')

invoice_pattern = r"Invoice No\.\s*:\s*(\d+)"
invoice_no = ''
for line in lines:
invoice_match = re.search(invoice_pattern, line)
if invoice_match:
invoice_no = invoice_match.group(1)
break

ordered_grade_column = 'ORDERED_GRADE'
ordered_quality_column = 'ORDERED_QUALITY'
ordered_grade_pattern = r"Material No\.\s*:\s*\d+\s*-\s*(.*?);"
ordered_quality_pattern = r"Material No\.\s*:\s*\d+\s*-\s*.*?;\s*(.*?)\s*,"
ordered_grade = ''
ordered_quality = ''
if len(lines) >= 7:
ordered_grade_match = re.search(ordered_grade_pattern, lines[6])
ordered_quality_match = re.search(ordered_quality_pattern, lines[6])
if ordered_grade_match:
ordered_grade = ordered_grade_match.group(1).strip()
if ordered_quality_match:
ordered_quality = ordered_quality_match.group(1).strip()

entity_line = lines[5].strip()
entity_match = re.search(r"Vendor No\.\s*:\s*(.*)", entity_line)
entity = entity_match.group(1).strip().split(' - ')[-1] if entity_match else ''

if 'UK' in entity:
entity = 'UK'
elif 'GREECE' in entity:
entity = 'GR'
elif 'ITALY' in entity:
entity = 'IT'
elif 'LAUSANNE' in entity:
entity = 'CH'
elif 'VIPA RECYCLING (IRELAND) LTD' in entity:
entity = 'IE'

mapped_data['Entity'] = [entity] * len(table_data)
mapped_data['Invoice No'] = [invoice_no] * len(table_data)
mapped_data[ordered_grade_column] = [ordered_grade] * len(table_data)
mapped_data[ordered_quality_column] = [ordered_quality] * len(table_data)

df = pd.DataFrame(mapped_data)
dfs.append(df)

result_df = pd.concat(dfs, ignore_index=True)

formatted_date = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')

# Construct the folder path with the date
folder_date = datetime.datetime.now().strftime('%Y%m%d')
folder_path = os.path.join(base_directory, 'claims', folder_date)

os.makedirs(folder_path, exist_ok=True)

# Construct the Excel file path
excel_filename = f'mycust{folder}_{formatted_date}.xlsx'
excel_filepath = os.path.join(folder_path, excel_filename)

# Add a new column with the name of the output Excel file
result_df['EXCEL_FILE_NAME'] = excel_filename

result_df['CONTAINER_NO'] = result_df['CONTAINER_NO'].apply(lambda x: re.search(r"/(.*?)/", x).group(1) if "/" in x else None)

result_df.to_excel(excel_filepath, index=False)



return excel_filepath

if __name__ == "__main__":
base_directory = 'mydirectory'

# Get the folder name from the user using the GUI
folder = get_folder_name()

if folder:
excel_filepath = process_pdfs_and_generate_excel(base_directory, folder)
if excel_filepath:
print(f"Excel file saved at: {excel_filepath}")

# Add an input prompt to keep the command prompt window open
input("Press Enter to exit...")


更多回答

Try to add option --console to pyinstaller call.

尝试向pyinstaller调用添加OPTION--CONSOLE。

if you do not know how to deal with pyinstaller you can use a helper like auto-py-to-exe library it will make it easy on you pypi.org/project/auto-py-to-exe

如果您不知道如何处理pyinstaller,您可以使用一个帮助程序,如自动y-to-exe库,这将使它对您的pypi.org/project/Auto-py-to-exe很容易

优秀答案推荐

__name__ is "__main__" only when the script is directly executed. Under an executable, chances are that it isn't using __name__ as __main__ and so the source inside may not be getting run.

只有当脚本直接执行时,__name__才是“__main__”。在可执行文件下,它可能没有使用__name__作为__main__,因此内部的源代码可能不会运行。


Try to eliminate that if __name__ == "main" check and it should be good if the rest steps are fine.

尝试取消IF__NAME__==“Main”检查,如果其余步骤都很好,则应该很好。


更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com