Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
product-uniqer
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Recolic
product-uniqer
Commits
18fa6f6a
There was an error fetching the commit references. Please try again later.
Commit
18fa6f6a
authored
2 years ago
by
Recolic K
Browse files
Options
Downloads
Patches
Plain Diff
new
parent
9b757288
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
look-for-files-for-excels-in-parent-dir/README.md
+8
-0
8 additions, 0 deletions
look-for-files-for-excels-in-parent-dir/README.md
look-for-files-for-excels-in-parent-dir/main.py
+94
-0
94 additions, 0 deletions
look-for-files-for-excels-in-parent-dir/main.py
with
102 additions
and
0 deletions
look-for-files-for-excels-in-parent-dir/README.md
0 → 100644
+
8
−
0
View file @
18fa6f6a
# 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.
This diff is collapsed.
Click to expand it.
look-for-files-for-excels-in-parent-dir/main.py
0 → 100644
+
94
−
0
View file @
18fa6f6a
#!/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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment