-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathspreadsheet.py
36 lines (29 loc) · 1.1 KB
/
spreadsheet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import logging
import openpyxl
from typing import List
# Setup logging
logger = logging.getLogger(__name__)
class SpreadsheetManager:
"""Manages reading and writing to Excel spreadsheets."""
def __init__(self, file_path: str):
self.file_path = file_path
self.workbook = openpyxl.load_workbook(file_path)
self.sheet = self.workbook.active
def read_data(self) -> List[List[str]]:
"""Reads data from the active sheet and returns it as a list of lists."""
data = []
for row in self.sheet.iter_rows(values_only=True):
data.append(list(row))
return data
def write_data(self, data: List[List[str]]) -> None:
"""Writes data to the active sheet."""
self.sheet.delete_rows(1, self.sheet.max_row)
for row in data:
self.sheet.append(row)
self.workbook.save(self.file_path)
def add_row(self, row: List[str]) -> None:
"""Adds a single row to the active sheet."""
self.sheet.append(row)
self.workbook.save(self.file_path)
def __del__(self):
self.workbook.close()