|
| 1 | +from sys import argv |
| 2 | +from pathlib import Path |
| 3 | +from glob import glob |
| 4 | +from collections import OrderedDict |
| 5 | + |
| 6 | +import xlwings as xw |
| 7 | + |
| 8 | +def merge_sheets(wb: xw.Book) -> None: |
| 9 | + sheets: list[xw.Sheet] = wb.sheets |
| 10 | + main_sheet: xw.Sheet = sheets[0] |
| 11 | + main_sheet_last_row: int = main_sheet.used_range.last_cell.row |
| 12 | + |
| 13 | + while len(sheets) > 1: |
| 14 | + second_sheet: xw.Sheet = sheets[1] |
| 15 | + second_sheet_last_row: int = second_sheet.used_range.last_cell.row |
| 16 | + second_sheet_last_column: int = second_sheet.used_range.last_cell.column |
| 17 | + second_sheet.range((2, 1), (second_sheet_last_row, second_sheet_last_column)).copy(destination=main_sheet.range((main_sheet_last_row + 1, 1))) |
| 18 | + second_sheet.delete() |
| 19 | + main_sheet_last_row: int = main_sheet.used_range.last_cell.row |
| 20 | + |
| 21 | +def main() -> None: |
| 22 | + |
| 23 | + argvs: list[str] = argv[1:] |
| 24 | + extracted_argvs: list[str] = [] |
| 25 | + |
| 26 | + # Check files quantity |
| 27 | + if len(argvs) < 1: raise FileNotFoundError |
| 28 | + |
| 29 | + # Extract wildcard to path strings |
| 30 | + for path in argvs: |
| 31 | + p: Path = Path(path).absolute() |
| 32 | + if p.stem == '*': |
| 33 | + glob_paths: list[str] = glob(str(p)) |
| 34 | + for extracted_path in glob_paths: |
| 35 | + extracted_argvs.append(extracted_path) |
| 36 | + else: |
| 37 | + extracted_argvs.append(path) |
| 38 | + |
| 39 | + |
| 40 | + # Check files existence |
| 41 | + for path in extracted_argvs: |
| 42 | + if not (Path(path).is_file() and Path(path).exists()): raise FileNotFoundError |
| 43 | + |
| 44 | + # Convert to absolute path |
| 45 | + absolute_paths: list[Path] = [Path(path).resolve() for path in extracted_argvs] |
| 46 | + # absolute_paths: list[Path] = list(map(lambda path: Path(path).resolve(), extracted_argvs)) |
| 47 | + |
| 48 | + # Deduplicate |
| 49 | + absolute_paths = list(OrderedDict.fromkeys(absolute_paths)) |
| 50 | + |
| 51 | + # print(absolute_paths) |
| 52 | + |
| 53 | + main_file_path: Path = absolute_paths[0] |
| 54 | + |
| 55 | + # Check files suffix |
| 56 | + allowed_suffix: tuple[str] = ('.xlsx', '.xls') |
| 57 | + for path in absolute_paths: |
| 58 | + if not path.suffix.lower() in allowed_suffix: raise FileNotFoundError |
| 59 | + |
| 60 | + with xw.App(add_book=False, visible=False) as app: |
| 61 | + main_wb: xw.Book = app.books.open(main_file_path) |
| 62 | + main_wb_sheet: xw.Sheet = main_wb.sheets[0] |
| 63 | + |
| 64 | + if len(main_wb.sheets) > 1: merge_sheets(main_wb) |
| 65 | + |
| 66 | + if len(absolute_paths) > 1: |
| 67 | + for path in absolute_paths[1:]: |
| 68 | + second_wb: xw.Book = app.books.open(path) |
| 69 | + if len(second_wb.sheets) > 1: merge_sheets(second_wb) |
| 70 | + |
| 71 | + second_wb_sheet: xw.Sheet = second_wb.sheets[0] |
| 72 | + second_wb_sheet.copy(after=main_wb_sheet) |
| 73 | + |
| 74 | + merge_sheets(main_wb) |
| 75 | + |
| 76 | + main_wb_sheet.name = main_wb_sheet.name + '.all' |
| 77 | + main_wb.save(main_file_path.with_suffix('.all.xlsx')) |
| 78 | + # breakpoint() |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments