51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
import re
|
|
import openpyxl
|
|
from openpyxl.cell import Cell
|
|
from typing import Union, Tuple, Iterator, IO
|
|
import locale
|
|
|
|
|
|
class XlsxReplacement:
|
|
def __init__(self, file: Union[str, IO]):
|
|
self.file = file
|
|
self.xlsx = openpyxl.load_workbook(file, read_only=True, data_only=True)
|
|
self.errors = []
|
|
self.reset_settings = {}
|
|
|
|
def get_replacements(self) -> Iterator[Tuple[int, int, str]]:
|
|
self._get_settings()
|
|
sheet = self.xlsx['replacements']
|
|
for row in sheet.iter_rows(min_row=1, max_col=5):
|
|
content = [c.value for c in row]
|
|
x = int(content[0])
|
|
y = int(content[1])
|
|
value = content[2]
|
|
if len(content) == 5:
|
|
value_format = content[3]
|
|
str_format = content[4]
|
|
print(value_format)
|
|
print(str_format)
|
|
if value_format is not None and isinstance(value_format, str) and len(value_format) > 0:
|
|
value = locale.format(value_format, value)
|
|
if str_format is not None and isinstance(str_format, str) and len(str_format) > 0:
|
|
value = str_format.format(value)
|
|
if not isinstance(value, str):
|
|
value = str(value)
|
|
yield x, y, value
|
|
|
|
def _get_settings(self):
|
|
sheet = self.xlsx['settings']
|
|
for row in sheet.iter_rows(max_col=2):
|
|
key, value = [i.value for i in row]
|
|
if key == 'locale':
|
|
print("Using locale %s" % value)
|
|
old_locale = '.'.join(locale.getlocale())
|
|
locale.setlocale(locale.LC_ALL, value)
|
|
self.reset_settings['locale'] = old_locale
|
|
|
|
def _reset_settings(self):
|
|
for key, value in self.reset_settings:
|
|
if key == 'locale':
|
|
locale.setlocale(locale.LC_ALL, value)
|
|
|