调整报告产品取值

This commit is contained in:
mgw
2025-04-08 09:48:05 +08:00
parent 80f259651c
commit f912a81e7b
2 changed files with 92 additions and 25 deletions

View File

@@ -73,7 +73,8 @@
<table class="table table-sm o_main_table mt-4" style="border: 1px solid black;">
<tr>
<td style="width: 15%; border: 1px solid black;"><strong>产品名称:</strong></td>
<td style="width: 35%; border: 1px solid black;"><span t-field="o.product_id.name"/></td>
<!-- <td style="width: 35%; border: 1px solid black;"><span t-field="o.product_id.name"/></td> -->
<td style="width: 35%; border: 1px solid black;"><span t-field="o.part_name"/></td>
<td style="width: 15%; border: 1px solid black;"><strong>材料:</strong></td>
<td style="width: 35%; border: 1px solid black;"><span t-field="o.material_name"/></td>
</tr>

View File

@@ -6,10 +6,7 @@ import win32gui
import win32con
import logging
import time
# 配置日志记录
logging.basicConfig(filename='service.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
import re
app = FastAPI()
@@ -19,11 +16,11 @@ class FileUploadRequest(BaseModel):
# FTP 服务器配置信息
ftp_host = '110.52.114.162'
ftp_port = 10021
ftp_host = '47.119.33.43'
ftp_port = 21
ftp_user = 'ftpuser'
ftp_password = '123456'
ftp_directory = '/home/ftp/ftp_root/ThreeTest/XT/Before/'
ftp_password = 'FTPftp123'
ftp_directory = 'ThreeTest/XT/Before/'
def find_child_window(parent_hwnd, class_name):
@@ -63,25 +60,95 @@ def find_child_window_by_title(parent_hwnd, title):
return child_hwnds
# 获取 ComboBox 的句柄
def get_combobox_handle(parent_handle):
combo_handle = win32gui.FindWindowEx(parent_handle, 0, "ComboBox", None)
if combo_handle == 0:
raise Exception("ComboBox not found")
return combo_handle
# 获取 ComboBox 中的所有选项
def get_combobox_items(combo_handle):
count = win32gui.SendMessage(combo_handle, win32con.CB_GETCOUNT, 0, 0)
items = []
for i in range(count):
length = win32gui.SendMessage(combo_handle, win32con.CB_GETLBTEXTLEN, i, 0)
buffer = win32gui.PyMakeBuffer(length + 1)
win32gui.SendMessage(combo_handle, win32con.CB_GETLBTEXT, i, buffer)
byte_data = buffer.tobytes()
# 尝试多种编码方式
text = None
# 尝试多种编码方式包括utf-16le
for encoding in ['utf-16le', 'utf-8', 'gbk', 'latin-1']:
try:
decoded_text = byte_data.decode(encoding).rstrip('\x00')
# 如果解码后的文本看起来是合理的,就接受它
if any(char.isprintable() for char in decoded_text):
text = decoded_text
break
except UnicodeDecodeError:
continue
# 如果所有解码方式都失败,或者内容仍有大量乱码,显示为十六进制字符串
if text is None or not all(char.isprintable() or char.isspace() for char in text):
text = byte_data.hex()
items.append(text)
return items
# 获取当前选定项
def get_combobox_selected(combo_handle):
index = win32gui.SendMessage(combo_handle, win32con.CB_GETCURSEL, 0, 0)
if index == -1:
return None
length = win32gui.SendMessage(combo_handle, win32con.CB_GETLBTEXTLEN, index, 0)
buffer = win32gui.PyMakeBuffer(1024) # 调整缓冲区大小
win32gui.SendMessage(combo_handle, win32con.CB_GETLBTEXT, index, buffer)
# 尝试多种编码方式进行解码
for encoding in ['utf-16le', 'utf-8', 'latin-1']:
try:
# 解码
raw_text = buffer.tobytes().decode(encoding, errors='ignore').rstrip('\x00')
# 使用正则表达式查找 "数字 + 月" 格式的部分
match = re.search(r'\d+月', raw_text)
if match:
return match.group()
# 使用正则表达式提取有效字符(中文、数字、字母和常见标点)
filtered_text = re.findall(r'[\w\u4e00-\u9fa5]+', raw_text)
# 返回匹配到的第一个有效部分
if filtered_text:
return filtered_text[0].strip()
except UnicodeDecodeError:
continue # 尝试下一个编码方式
# 如果所有解码方式都失败,返回 None
return None
# 设置 ComboBox 的值
def set_combobox_value(combo_handle, value):
items = get_combobox_items(combo_handle)
try:
index = items.index(value)
win32gui.SendMessage(combo_handle, win32con.CB_SETCURSEL, index, 0)
except ValueError:
raise Exception("Value not found in ComboBox")
def set_path_and_save(filename):
parent_hwnd = win32gui.FindWindow(None, '另存为')
parent_hwnd = win32gui.FindWindow(None, '保存Excel文件')
if parent_hwnd == 0:
raise HTTPException(status_code=404, detail="没有找到保存报告的窗口,请检查!")
# 这里假设“地址:”是你需要的部分标题
address_hwnds = find_child_window_by_partial_title(parent_hwnd, "地址:")
# 确保找到的窗口句柄有效
if not address_hwnds:
raise HTTPException(status_code=404, detail="未找到地址框,请联系管理员!")
# 假设找到的第一个窗口是目标组件
address_hwnd = address_hwnds[0]
logging.info(f"找到地址框地址: {win32gui.GetWindowText(address_hwnd)}")
# 设置路径
local_file_path = os.path.join(win32gui.GetWindowText(address_hwnd).split(' ')[1], filename)
combo_handle = get_combobox_handle(parent_hwnd)
#logging.info(f"ComboBox Items: {get_combobox_items(combo_handle)}")
logging.info(f"Current Selected: {get_combobox_selected(combo_handle)}")
local_file_path = "C:\\RationalDMIS64\\Output\\" + get_combobox_selected(combo_handle) + "\\" + filename
logging.info(f"设置路径: {local_file_path}")
path_hwnds = find_child_window(parent_hwnd, 'Edit')
@@ -115,7 +182,6 @@ def wait_for_file_to_save(filepath, timeout=30):
def upload_file_to_ftp(local_file):
if not os.path.isfile(local_file):
raise HTTPException(status_code=204, detail="文件未找到")
@@ -157,4 +223,4 @@ async def upload_file(request: FileUploadRequest):
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
uvicorn.run(app, host="0.0.0.0", port=8999)