initial commit
This commit is contained in:
12
.gitignore
vendored
Normal file
12
.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# python generated files
|
||||
__pycache__/
|
||||
*.py[oc]
|
||||
build/
|
||||
dist/
|
||||
wheels/
|
||||
*.egg-info
|
||||
|
||||
# venv
|
||||
.venv
|
||||
|
||||
.DS_Store
|
||||
1
.python-version
Normal file
1
.python-version
Normal file
@@ -0,0 +1 @@
|
||||
3.12.2
|
||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# xlsx-clean
|
||||
|
||||
Create a new xlsx file from the last file in a dir and clear specified cells.
|
||||
28
pyproject.toml
Normal file
28
pyproject.toml
Normal file
@@ -0,0 +1,28 @@
|
||||
[project]
|
||||
name = "xlsx-clean"
|
||||
version = "0.1.0"
|
||||
description = "Add your description here"
|
||||
authors = [
|
||||
{ name = "Diaco Soltanpour", email = "dyaco8464@yahoo.com" }
|
||||
]
|
||||
dependencies = [
|
||||
"openpyxl>=3.1.2",
|
||||
"beaupy>=3.8.2",
|
||||
"pandas>=2.2.2",
|
||||
]
|
||||
readme = "README.md"
|
||||
requires-python = ">= 3.12"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.rye]
|
||||
managed = true
|
||||
dev-dependencies = []
|
||||
|
||||
[tool.hatch.metadata]
|
||||
allow-direct-references = true
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/xlsx_clean"]
|
||||
44
requirements-dev.lock
Normal file
44
requirements-dev.lock
Normal file
@@ -0,0 +1,44 @@
|
||||
# generated by rye
|
||||
# use `rye lock` or `rye sync` to update this lockfile
|
||||
#
|
||||
# last locked with the following flags:
|
||||
# pre: false
|
||||
# features: []
|
||||
# all-features: false
|
||||
# with-sources: false
|
||||
|
||||
-e file:.
|
||||
beaupy==3.8.2
|
||||
# via xlsx-clean
|
||||
emoji==2.11.0
|
||||
# via beaupy
|
||||
et-xmlfile==1.1.0
|
||||
# via openpyxl
|
||||
markdown-it-py==3.0.0
|
||||
# via rich
|
||||
mdurl==0.1.2
|
||||
# via markdown-it-py
|
||||
numpy==1.26.4
|
||||
# via pandas
|
||||
openpyxl==3.1.2
|
||||
# via xlsx-clean
|
||||
pandas==2.2.2
|
||||
# via xlsx-clean
|
||||
pygments==2.17.2
|
||||
# via rich
|
||||
python-dateutil==2.9.0.post0
|
||||
# via pandas
|
||||
python-yakh==0.3.2
|
||||
# via beaupy
|
||||
# via questo
|
||||
pytz==2024.1
|
||||
# via pandas
|
||||
questo==0.2.3
|
||||
# via beaupy
|
||||
rich==13.7.1
|
||||
# via beaupy
|
||||
# via questo
|
||||
six==1.16.0
|
||||
# via python-dateutil
|
||||
tzdata==2024.1
|
||||
# via pandas
|
||||
44
requirements.lock
Normal file
44
requirements.lock
Normal file
@@ -0,0 +1,44 @@
|
||||
# generated by rye
|
||||
# use `rye lock` or `rye sync` to update this lockfile
|
||||
#
|
||||
# last locked with the following flags:
|
||||
# pre: false
|
||||
# features: []
|
||||
# all-features: false
|
||||
# with-sources: false
|
||||
|
||||
-e file:.
|
||||
beaupy==3.8.2
|
||||
# via xlsx-clean
|
||||
emoji==2.11.0
|
||||
# via beaupy
|
||||
et-xmlfile==1.1.0
|
||||
# via openpyxl
|
||||
markdown-it-py==3.0.0
|
||||
# via rich
|
||||
mdurl==0.1.2
|
||||
# via markdown-it-py
|
||||
numpy==1.26.4
|
||||
# via pandas
|
||||
openpyxl==3.1.2
|
||||
# via xlsx-clean
|
||||
pandas==2.2.2
|
||||
# via xlsx-clean
|
||||
pygments==2.17.2
|
||||
# via rich
|
||||
python-dateutil==2.9.0.post0
|
||||
# via pandas
|
||||
python-yakh==0.3.2
|
||||
# via beaupy
|
||||
# via questo
|
||||
pytz==2024.1
|
||||
# via pandas
|
||||
questo==0.2.3
|
||||
# via beaupy
|
||||
rich==13.7.1
|
||||
# via beaupy
|
||||
# via questo
|
||||
six==1.16.0
|
||||
# via python-dateutil
|
||||
tzdata==2024.1
|
||||
# via pandas
|
||||
2
src/xlsx_clean/__init__.py
Normal file
2
src/xlsx_clean/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def hello() -> str:
|
||||
return "Hello from xlsx-clean!"
|
||||
109
src/xlsx_clean/clean_cells.py
Normal file
109
src/xlsx_clean/clean_cells.py
Normal file
@@ -0,0 +1,109 @@
|
||||
import sys
|
||||
from openpyxl import Workbook, load_workbook
|
||||
import time
|
||||
from beaupy import confirm, prompt, select, select_multiple
|
||||
|
||||
# from beaupy.spinners import *
|
||||
from rich.console import Console
|
||||
import glob
|
||||
import fnmatch
|
||||
import pathlib
|
||||
import os
|
||||
import pandas
|
||||
|
||||
NEW_VALUE = ""
|
||||
# load questions from text file
|
||||
with open("src/xlsx_clean/strings.txt") as f:
|
||||
content = f.readlines()
|
||||
# remove whitespace characters like `\n` at the end of each line
|
||||
content = [x.strip() for x in content]
|
||||
q1 = content[0]
|
||||
q2 = content[1]
|
||||
q3 = content[2]
|
||||
|
||||
path_df = pandas.read_csv("src/xlsx_clean/file_data.csv")
|
||||
path_df["set_dir"] = [pathlib.Path(path).parent.parent for path in path_df["dir"]]
|
||||
path_list = [pathlib.Path(path) for path in path_df["dir"]]
|
||||
parent_path = list(
|
||||
path_df.drop_duplicates(subset=["set_name"], keep="first")["set_name"]
|
||||
)
|
||||
|
||||
# get parent path index
|
||||
console = Console()
|
||||
console.print(q1)
|
||||
selected_set = select(parent_path, cursor_style="cyan")
|
||||
|
||||
console.print(q2)
|
||||
selected_dir = select(
|
||||
[
|
||||
pathlib.Path(path).stem
|
||||
for path in list(path_df[path_df["set_name"] == selected_set]["dir"])
|
||||
]
|
||||
)
|
||||
console.print(f"Selected: {selected_dir}")
|
||||
path_ = pathlib.Path(
|
||||
path_df[
|
||||
(path_df["set_name"] == selected_set)
|
||||
& (path_df["dir"].str.endswith(selected_dir))
|
||||
]["dir"].iloc[0]
|
||||
)
|
||||
pattern = path_df[
|
||||
(path_df["set_name"] == selected_set) & (path_df["dir"].str.endswith(selected_dir))
|
||||
]["pattern"].iloc[0]
|
||||
search_pattern = pattern.replace("[SERIAL]", "*")
|
||||
names = path_.glob(search_pattern)
|
||||
files = [str(x) for x in names if x.is_file()]
|
||||
|
||||
batch_serial = 0
|
||||
# Prompt for serial
|
||||
batch_serial = prompt(q3, target_type=str)
|
||||
|
||||
|
||||
def find_last_workbook(files):
|
||||
# Sort the list of files by their names
|
||||
sorted_files = sorted(files)
|
||||
|
||||
# The last file will be the one at the end of the sorted list
|
||||
last_file = sorted_files[-1]
|
||||
|
||||
return last_file
|
||||
|
||||
|
||||
def get_workbook_names(files, batch_serial):
|
||||
last_workbook = find_last_workbook(files)
|
||||
return last_workbook, path_ / pattern.replace("[SERIAL]", batch_serial)
|
||||
|
||||
|
||||
ref_workbook_name, new_workbook_name = get_workbook_names(files, batch_serial)
|
||||
# Selected Excel file
|
||||
console.print(f"Selected: {pathlib.Path(ref_workbook_name).stem}")
|
||||
workbook = load_workbook(ref_workbook_name)
|
||||
cells_to_clear = path_df[
|
||||
(path_df["set_name"] == selected_set) & (path_df["dir"].str.endswith(selected_dir))
|
||||
]["cells_to_clear"].iloc[0]
|
||||
for workbook_data in cells_to_clear.split(","):
|
||||
worksheet_data = workbook_data.split("!")
|
||||
worksheet = workbook[worksheet_data[0].replace("'", "")]
|
||||
is_range = len(worksheet_data[1].split(":")) > 1
|
||||
if is_range:
|
||||
cell_range = worksheet[worksheet_data[1]]
|
||||
# Assign singular value to all cells
|
||||
for cell in cell_range:
|
||||
# Writes a new value PRESERVING cell styles.
|
||||
cell[0].value = NEW_VALUE
|
||||
else:
|
||||
worksheet[worksheet_data[1]].value = NEW_VALUE
|
||||
|
||||
serial_cell = path_df[
|
||||
(path_df["set_name"] == selected_set) & (path_df["dir"].str.endswith(selected_dir))
|
||||
]["serial_cell"].iloc[0]
|
||||
for workbook_data in serial_cell.split(","):
|
||||
worksheet_data = workbook_data.split("!")
|
||||
worksheet = workbook[worksheet_data[0].replace("'", "")]
|
||||
worksheet[worksheet_data[1]].value = batch_serial
|
||||
if not new_workbook_name.is_file():
|
||||
workbook.save(new_workbook_name)
|
||||
if os.name == "nt":
|
||||
os.system("start excel.exe new_workbook_name")
|
||||
else:
|
||||
console.print("Error: File already exists.")
|
||||
4
src/xlsx_clean/file_data.csv
Normal file
4
src/xlsx_clean/file_data.csv
Normal file
@@ -0,0 +1,4 @@
|
||||
dir,pattern,set_name,cells_to_clear,serial_cell
|
||||
"/Users/diaco/Downloads/inco/Quality control data sheet/2024/ICJ407 Brown",ICJ_407 op. [SERIAL].xlsx","inco",""
|
||||
"/Users/diaco/Downloads/Kimia Razi/1403/KRBR1200T Brown","ICJ308[SERIAL].xlsx","Kimia Razi","Milling!C3,Milling!C10:C12,Milling!C15,Milling!F8:J8,Milling!F11:J12,Batch!C14,Batch!C18:C19,Batch!G11,Batch!G14,Batch!G18,Batch!I11,Batch!I14,Batch!I18,Batch!K11:M11,Batch!K14:M14,Batch!K18:M18,Batch!B20,'KW & Temp'!A8:I8","Batch!C2"
|
||||
"/Users/diaco/Downloads/Kimia Razi/1403/KRCY1100T Blue","KRCY1100T Blue S. [SERIAL].xlsx","Kimia Razi","Milling!C3,Milling!C10:C12,Milling!C15,Milling!F8:J8,Milling!F11:J12,Batch!C14,Batch!C18:C19,Batch!G11,Batch!G14,Batch!G18,Batch!I11,Batch!I14,Batch!I18,Batch!K11:M11,Batch!K14:M14,Batch!K18:M18,Batch!B20,'KW & Temp'!A8:I8","Batch!C2"
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 101.
|
3
src/xlsx_clean/strings.txt
Normal file
3
src/xlsx_clean/strings.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Select Set
|
||||
Select Ink Color
|
||||
Serial:
|
||||
Reference in New Issue
Block a user