This commit is contained in:
alihajiali 2024-12-26 11:53:26 +03:30
commit b140f6d065
5 changed files with 231 additions and 0 deletions

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
et_xmlfile==2.0.0
openpyxl==3.1.5

173
.gitignore vendored Normal file
View File

@ -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

BIN
1.xlsx Normal file

Binary file not shown.

10
README.md Normal file
View File

@ -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)
```

46
xl/main.py Normal file
View File

@ -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