commit b140f6d065c46743cc394371fbd4f6e4fb86a2ff Author: alihajiali Date: Thu Dec 26 11:53:26 2024 +0330 c diff --git a/ requirements.txt b/ requirements.txt new file mode 100644 index 0000000..24d5b06 --- /dev/null +++ b/ requirements.txt @@ -0,0 +1,2 @@ +et_xmlfile==2.0.0 +openpyxl==3.1.5 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a86b1e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,173 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST +requirements.txt +setup.py +ultralytics.egg-info + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other info into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +mlruns/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# Profiling +*.pclprof + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +.idea +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# VSCode project settings +.vscode/ + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# datasets and projects (ignore /datasets dir at root only to allow for docs/en/datasets dir) +/datasets +runs/ +wandb/ +.DS_Store + +# Neural Network weights ----------------------------------------------------------------------------------------------- +weights/ +*.weights +*.pt +*.pb +*.onnx +*.engine +*.mlmodel +*.mlpackage +*.torchscript +*.tflite +*.h5 +*.mnn +*_saved_model/ +*_web_model/ +*_openvino_model/ +*_paddle_model/ +*_ncnn_model/ +*_imx_model/ +pnnx* + +# Autogenerated files for tests +/ultralytics/assets/ + +# calibration image +calibration_*.npy \ No newline at end of file diff --git a/1.xlsx b/1.xlsx new file mode 100644 index 0000000..4872ac6 Binary files /dev/null and b/1.xlsx differ diff --git a/README.md b/README.md new file mode 100644 index 0000000..006d9ab --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +example usage : + + +``` python +file_path = "./1.xlsx" +sheet_name = "Sheet" +with XL(file_path) as excel_to_dict: + data = excel_to_dict.sheet_to_dict(sheet_name) +print(data) +``` \ No newline at end of file diff --git a/xl/main.py b/xl/main.py new file mode 100644 index 0000000..7e63bcb --- /dev/null +++ b/xl/main.py @@ -0,0 +1,46 @@ +import openpyxl + +class XL: + def __init__(self, file_path: str): + """ + Initialize the class with the path to the Excel file. + """ + self.file_path = file_path + self.workbook = None + + def __enter__(self): + """ + Open the workbook when entering the context. + """ + self.workbook = openpyxl.load_workbook(self.file_path) + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + """ + Close the workbook when exiting the context. + """ + if self.workbook: + self.workbook.close() + + def sheet_to_dict(self, sheet_name: str): + """ + Convert a sheet's data to a dictionary, removing rows with None values. + """ + if not self.workbook: + raise ValueError("Workbook is not loaded. Ensure you use 'with' to open the file.") + + if sheet_name not in self.workbook.sheetnames: + raise ValueError(f"Sheet '{sheet_name}' not found in the workbook.") + + sheet = self.workbook[sheet_name] + headers = [cell.value for cell in sheet[1] if cell.value is not None] + + data_list = [] + for row in sheet.iter_rows(min_row=2, values_only=True): + row_dict = {headers[i]: cell for i, cell in enumerate(row) if i < len(headers)} + # Remove keys with None values + row_dict = {key: value for key, value in row_dict.items() if value is not None} + if row_dict: # Only include non-empty rows + data_list.append(row_dict) + + return data_list \ No newline at end of file