From 18fa6f6a5bee8d7b6ab2d4ba74ab5d12690ae4b5 Mon Sep 17 00:00:00 2001
From: Recolic K <root@recolic.net>
Date: Sun, 21 Aug 2022 23:08:04 +0800
Subject: [PATCH] new

---
 .../README.md                                 |  8 ++
 .../main.py                                   | 94 +++++++++++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 look-for-files-for-excels-in-parent-dir/README.md
 create mode 100644 look-for-files-for-excels-in-parent-dir/main.py

diff --git a/look-for-files-for-excels-in-parent-dir/README.md b/look-for-files-for-excels-in-parent-dir/README.md
new file mode 100644
index 0000000..3766add
--- /dev/null
+++ b/look-for-files-for-excels-in-parent-dir/README.md
@@ -0,0 +1,8 @@
+# usage
+
+run this program in a directory. This directory contains some XLSX tables. 
+
+In every xlsx, there is one colume with a cell `零件名称`. This column contains more non-empty cells 'A', 'B', 'C', 'D'...
+
+This program creates a folder for every xlsx, and copys `../A.*`, `../B.*`, ... into that folder. 
+
diff --git a/look-for-files-for-excels-in-parent-dir/main.py b/look-for-files-for-excels-in-parent-dir/main.py
new file mode 100644
index 0000000..1e1100a
--- /dev/null
+++ b/look-for-files-for-excels-in-parent-dir/main.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python3
+# A script to deal with product sheet. (see test-*.csv)
+import traceback
+def show_exception_and_exit(exc_type, exc_value, tb):
+    traceback.print_exception(exc_type, exc_value, tb)
+    input("按回车键退出")
+    sys.exit(-1)
+
+import sys
+import os
+if os.name == 'nt':
+    sys.excepthook = show_exception_and_exit
+
+
+#########################################################################
+
+import openpyxl
+import os, shutil
+
+def list_partnames_to_lookfor(xlsxPath):
+    # returns a list of partnames found
+    xls = openpyxl.load_workbook(xlsxPath)
+    xls_sheet = xls.worksheets[0]
+
+    name_colnum = -1
+    NAME_COLNAME = '零件名称'
+    result_list = []
+    for rownum, row in enumerate(xls_sheet.rows):
+        if name_colnum != -1:
+            partname = row[name_colnum].value
+            if partname is not None:
+                if partname is not str:
+                    partname = str(partname)
+                result_list.append(partname.strip())
+        else:
+            for colnum, ele in enumerate(row):
+                # if ele.value is not None:
+                #     print("TRY: ", ele.value)
+                if ele.value is str and ele.value.strip() == NAME_COLNAME.strip():
+                    name_colnum = colnum
+                    break
+    if name_colnum == -1:
+        raise RuntimeError('表格中未找到以下内容单元格: ' + NAME_COLNAME)
+
+    return result_list
+
+
+def copy_files_from_dir_to_dir_with_displayname(fromDir, toDir, fname_list_without_ext):
+    # todir must exist
+    # returns a list of filenames that never used
+    mkrelpath = lambda fname: fromDir + os.path.sep + fname
+    files = [f for f in os.listdir(fromDir) if os.path.isfile(mkrelpath(f))]
+    never_used_fname_list = fname_list_without_ext
+    for f in files:
+        fname_without_ext = os.path.splitext(f)[0].strip()
+        if fname_without_ext in fname_list_without_ext:
+            # hit
+            shutil.copy(mkrelpath(f), toDir)
+            print("COPY from " + mkrelpath(f) + " TO " + toDir)
+            if fname_without_ext in never_used_fname_list:
+                never_used_fname_list = [ unused_fname for unused_fname in never_used_fname_list if unused_fname != fname_without_ext ]
+    return never_used_fname_list
+
+
+
+def main():
+    print("DEBUG: workdir=", os.getcwd())
+    is_xlsx = lambda fname: (fname.endswith('.xlsm') or fname.endswith('.xlsx') or fname.endswith('.xls') or fname.endswith('.XLSM') or fname.endswith('.XLSX') or fname.endswith('.XLS')) and (not fname.startswith('~$'))
+    files = [f for f in os.listdir('.') if os.path.isfile(f)]
+    missing_parts = []
+    for f in files:
+        if is_xlsx(f):
+            print("WORKING ON ", f)
+            fname_without_ext = os.path.splitext(f)[0].strip()
+            dest_dirname = fname_without_ext
+            if os.path.isdir(dest_dirname):
+                shutil.rmtree(dest_dirname)
+            os.mkdir(dest_dirname)
+
+            partnames = list_partnames_to_lookfor(f)
+            print("DEBUG: partnames=", partnames)
+            mpthis = copy_files_from_dir_to_dir_with_displayname('..', dest_dirname, partnames)
+            print("DEBUG: missing_part_this=", mpthis)
+            missing_parts += mpthis
+    if len(missing_parts) > 0:
+        with open('缺失零件.csv', 'w+') as f:
+            f.write('\n'.join(missing_parts))
+
+
+main()
+
+
+
+
-- 
GitLab