jumpserver/apps/common/drf/parsers/excel.py

19 lines
525 B
Python
Raw Normal View History

import pyexcel
2023-03-13 10:06:54 +00:00
from django.utils.translation import gettext as _
2023-03-13 09:57:50 +00:00
from .base import BaseFileParser
class ExcelFileParser(BaseFileParser):
media_type = 'text/xlsx'
def generate_rows(self, stream_data):
2023-03-13 09:57:50 +00:00
try:
workbook = pyexcel.get_book(file_type='xlsx', file_content=stream_data)
except Exception:
2023-03-13 10:06:54 +00:00
raise Exception(_('Invalid excel file'))
# 默认获取第一个工作表sheet
sheet = workbook.sheet_by_index(0)
rows = sheet.rows()
return rows