diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-09-08 15:37:08 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-09-08 15:37:08 +0800 |
commit | 010d65d7188fb806733d7fe7deb6c3e3117b4537 (patch) | |
tree | 4cbd784969dd47cd93f69422f7c9a309b6774d27 | |
parent | 3b900d691b44a9ff1fd2c69043e2b624eded71bd (diff) |
Finds sheet by name, return compressed path
-rwxr-xr-x | compress.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/compress.py b/compress.py index 9e92790..816a872 100755 --- a/compress.py +++ b/compress.py @@ -20,11 +20,19 @@ def wrap_districts(districts, length=32): return lines -def compress(filepath): +def compress(filepath: str) -> str: regions = {} # all high/middle risk regions go here # open xlsx file xlsx = load_workbook(filepath) - lst = xlsx.worksheets[0] # "列表" + lst = None + for sheet in xlsx.worksheets: + if sheet.title == "列表": + lst = sheet + break + else: + # sheet not found + raise Exception("未找到名为“列表”的表格") + # B3:Dxxxx for province, city, district in lst.iter_rows( min_row=3, max_row=None, min_col=2, max_col=4, values_only=True @@ -81,7 +89,9 @@ def compress(filepath): ws.cell(row=current_row, column=col).border = top_border current_row += len(dist_lines) - wb.save(filepath.replace(".xlsx", "(压缩).xlsx")) + compressed_filepath = filepath.replace(".xlsx", "(压缩).xlsx") + wb.save(compressed_filepath) + return compressed_filepath if __name__ == "__main__": |