base_dir = os.path.dirname(__file__)
file_path = os.path.join(base_dir, '文件名')


class XlsHandler:
    """
    处理 xlsx 文件
    """

    def __init__(self, file) -> None:
        self.wb = xlrd.open_workbook(file_contents=file.read())
        self.sheet = self.wb.sheet_by_index(0)
        self.header = self.sheet.row_values(0)
        self.nrows = self.sheet.nrows

    def to_datalist(self, start: int = 1) -> list:
        """
        解析数据为 表头作为键的字典 列表
        :return:
        """
        header = self.header
        return [
            {
                str(col).strip(): self.sheet.cell(row, i).value
                for i, col in enumerate(header)
            }
            for row in range(start, self.sheet.nrows)
        ]


class Command():

    def handle_excl(self, ):
        xsl = XlsHandler(open(file_path, 'rb'))
        data = xsl.to_datalist()
        for d in data:
            yield d

  
  
   def data_list(self) -> List:
n = 0
error_list = []
data = self.handle_excl()
for d in data:
pass
return error_list

 

posted on 2022-08-08 15:40  輪滑少年  阅读(32)  评论(0编辑  收藏  举报