diff --git a/apps/accounts/automations/backup_account/handlers.py b/apps/accounts/automations/backup_account/handlers.py index df801cda0..763428308 100644 --- a/apps/accounts/automations/backup_account/handlers.py +++ b/apps/accounts/automations/backup_account/handlers.py @@ -3,13 +3,13 @@ import time from collections import defaultdict, OrderedDict from django.conf import settings -from openpyxl import Workbook from rest_framework import serializers +from xlsxwriter import Workbook from accounts.const.automation import AccountBackupType +from accounts.models.automations.backup_account import AccountBackupAutomation from accounts.notifications import AccountBackupExecutionTaskMsg, AccountBackupByObjStorageExecutionTaskMsg from accounts.serializers import AccountSecretSerializer -from accounts.models.automations.backup_account import AccountBackupAutomation from assets.const import AllTypes from common.utils.file import encrypt_and_compress_zip_file, zip_files from common.utils.timezone import local_now_filename, local_now_display @@ -144,10 +144,11 @@ class AccountBackupHandler: wb = Workbook(filename) for sheet, data in data_map.items(): - ws = wb.create_sheet(str(sheet)) + ws = wb.add_worksheet(str(sheet)) for row in data: - ws.append(row) - wb.save(filename) + for col, _data in enumerate(row): + ws.write_string(0, col, _data) + wb.close() files.append(filename) timedelta = round((time.time() - time_start), 2) print('创建备份文件完成: 用时 {}s'.format(timedelta)) diff --git a/apps/accounts/automations/change_secret/manager.py b/apps/accounts/automations/change_secret/manager.py index 46d3ef2b6..74dfc717e 100644 --- a/apps/accounts/automations/change_secret/manager.py +++ b/apps/accounts/automations/change_secret/manager.py @@ -4,7 +4,7 @@ from copy import deepcopy from django.conf import settings from django.utils import timezone -from openpyxl import Workbook +from xlsxwriter import Workbook from accounts.const import AutomationTypes, SecretType, SSHKeyStrategy, SecretStrategy from accounts.models import ChangeSecretRecord @@ -227,8 +227,9 @@ class ChangeSecretManager(AccountBasePlaybookManager): rows.insert(0, header) wb = Workbook(filename) - ws = wb.create_sheet('Sheet1') + ws = wb.add_worksheet('Sheet1') for row in rows: - ws.append(row) - wb.save(filename) + for col, data in enumerate(row): + ws.write_string(0, col, data) + wb.close() return True diff --git a/poetry.lock b/poetry.lock index 3bf68f9c9..4b7cac7ea 100644 --- a/poetry.lock +++ b/poetry.lock @@ -7540,6 +7540,22 @@ type = "legacy" url = "https://pypi.tuna.tsinghua.edu.cn/simple" reference = "tsinghua" +[[package]] +name = "xlsxwriter" +version = "3.1.9" +description = "A Python module for creating Excel XLSX files." +optional = false +python-versions = ">=3.6" +files = [ + {file = "XlsxWriter-3.1.9-py3-none-any.whl", hash = "sha256:b61c1a0c786f82644936c0936ec96ee96cd3afb9440094232f7faef9b38689f0"}, + {file = "XlsxWriter-3.1.9.tar.gz", hash = "sha256:de810bf328c6a4550f4ffd6b0b34972aeb7ffcf40f3d285a0413734f9b63a929"}, +] + +[package.source] +type = "legacy" +url = "https://pypi.tuna.tsinghua.edu.cn/simple" +reference = "tsinghua" + [[package]] name = "xmlsec" version = "1.3.13" @@ -7755,4 +7771,4 @@ reference = "tsinghua" [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "397cb294c81da3ce74f1a1c3044bd778669284c0c280177162ed9d51e623a7c8" +content-hash = "19950a5300f8a4425e3e3c9a096fc3782cb544e02152e92ac1bc09d1d9b5274a" diff --git a/pyproject.toml b/pyproject.toml index eff9f4444..58b9991f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -145,6 +145,7 @@ user-agents = "^2.2.0" django-cors-headers = "^4.3.0" mistune = "0.8.4" openai = "^1.3.7" +xlsxwriter = "^3.1.9" [tool.poetry.group.xpack.dependencies]