From 94e99a5b72a0eeb5142600e8ebec8143e01c301f Mon Sep 17 00:00:00 2001 From: Diaco Soltanpour Date: Thu, 11 Apr 2024 21:25:37 +0330 Subject: [PATCH] initial commit --- .gitignore | 12 ++++ .python-version | 1 + README.md | 3 + pyproject.toml | 28 +++++++++ requirements-dev.lock | 44 ++++++++++++++ requirements.lock | 44 ++++++++++++++ src/xlsx_clean/__init__.py | 2 + src/xlsx_clean/clean_cells.py | 109 ++++++++++++++++++++++++++++++++++ src/xlsx_clean/file_data.csv | 4 ++ src/xlsx_clean/strings.txt | 3 + 10 files changed, 250 insertions(+) create mode 100644 .gitignore create mode 100644 .python-version create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 requirements-dev.lock create mode 100644 requirements.lock create mode 100644 src/xlsx_clean/__init__.py create mode 100644 src/xlsx_clean/clean_cells.py create mode 100644 src/xlsx_clean/file_data.csv create mode 100644 src/xlsx_clean/strings.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..de84547 --- /dev/null +++ b/.gitignore @@ -0,0 +1,12 @@ +# python generated files +__pycache__/ +*.py[oc] +build/ +dist/ +wheels/ +*.egg-info + +# venv +.venv + +.DS_Store \ No newline at end of file diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..8531a3b --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.12.2 diff --git a/README.md b/README.md new file mode 100644 index 0000000..c9c2fc1 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# xlsx-clean + +Create a new xlsx file from the last file in a dir and clear specified cells. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..25ad619 --- /dev/null +++ b/pyproject.toml @@ -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"] diff --git a/requirements-dev.lock b/requirements-dev.lock new file mode 100644 index 0000000..a54a936 --- /dev/null +++ b/requirements-dev.lock @@ -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 diff --git a/requirements.lock b/requirements.lock new file mode 100644 index 0000000..a54a936 --- /dev/null +++ b/requirements.lock @@ -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 diff --git a/src/xlsx_clean/__init__.py b/src/xlsx_clean/__init__.py new file mode 100644 index 0000000..c77c419 --- /dev/null +++ b/src/xlsx_clean/__init__.py @@ -0,0 +1,2 @@ +def hello() -> str: + return "Hello from xlsx-clean!" \ No newline at end of file diff --git a/src/xlsx_clean/clean_cells.py b/src/xlsx_clean/clean_cells.py new file mode 100644 index 0000000..e108a14 --- /dev/null +++ b/src/xlsx_clean/clean_cells.py @@ -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.") diff --git a/src/xlsx_clean/file_data.csv b/src/xlsx_clean/file_data.csv new file mode 100644 index 0000000..0208ea4 --- /dev/null +++ b/src/xlsx_clean/file_data.csv @@ -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" \ No newline at end of file diff --git a/src/xlsx_clean/strings.txt b/src/xlsx_clean/strings.txt new file mode 100644 index 0000000..f22db5a --- /dev/null +++ b/src/xlsx_clean/strings.txt @@ -0,0 +1,3 @@ +Select Set +Select Ink Color +Serial: \ No newline at end of file