From 4513fc7036c4176ae4077a47e7daaa56a674f839 Mon Sep 17 00:00:00 2001 From: ChristopheSeux Date: Tue, 18 Jul 2023 17:09:27 +0200 Subject: [PATCH] add export xls --- operators/spreadsheet.py | 27 ++++++++++++++++++--------- ui/properties.py | 2 +- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/operators/spreadsheet.py b/operators/spreadsheet.py index 27ed397..ba36fec 100644 --- a/operators/spreadsheet.py +++ b/operators/spreadsheet.py @@ -476,7 +476,7 @@ class VSETB_OT_export_spreadsheet(Operator): settings = get_scene_settings() project = settings.active_project - return context.window_manager.invoke_props_dialog(self) + return context.window_manager.invoke_props_dialog(self, width=375) def draw(self, context): scn = context.scene @@ -642,13 +642,14 @@ class VSETB_OT_export_spreadsheet(Operator): try: import openpyxl except ModuleNotFoundError: - self.report({'INFO'}, 'Installing openpyxl') + self.report({'INFO'}, 'Installing openpyxl...') openpyxl = install_module('openpyxl') from openpyxl import Workbook workbook = Workbook() worksheet = workbook.active + workbook.active.title = 'BKL' for row in rows: worksheet.append(row) @@ -656,16 +657,24 @@ class VSETB_OT_export_spreadsheet(Operator): letter = col[0].column_letter worksheet.column_dimensions[letter].auto_size = True - # Save the file workbook.save(str(export_path)) - # elif spreadsheet.format == 'xls': - # try: - # import xlwt - # except ModuleNotFoundError: - # self.report({'INFO'}, 'Installing openpyxl') - # openpyxl = install_module('xlwt') + elif spreadsheet.format == 'xls': + try: + import xlwt + except ModuleNotFoundError: + self.report({'INFO'}, 'Installing xlwt...') + xlwt = install_module('xlwt') + + workbook = xlwt.Workbook() + worksheet = workbook.add_sheet('BKL') + + for row_index, row in enumerate(rows): + for col_index, cell_value in enumerate(row): + worksheet.write(row_index, col_index, cell_value) + + workbook.save(str(export_path)) elif spreadsheet.format == 'Clipboard': csv_buffer = StringIO() diff --git a/ui/properties.py b/ui/properties.py index 7abe363..633b381 100644 --- a/ui/properties.py +++ b/ui/properties.py @@ -189,7 +189,7 @@ def get_custom_name_items(self, context): class SpreadsheetExport(PropertyGroup): use_custom_cells: BoolProperty(default=False) - format : EnumProperty(items=[(i, i, '') for i in ('csv', 'xlsx', 'Clipboard')]) + format : EnumProperty(items=[(i, i, '') for i in ('csv', 'xlsx', 'xls', 'Clipboard')]) separator : StringProperty(default='\\n') delimiter : StringProperty(default=';') export_path : StringProperty(default='//export')