从现有工作簿表中删除行和列

我试图从sheet除前三列和从坐标 (U - AA) 开始的所有列之外的所有填充行中删除。除了最后的标题,我必须删除所有填写的信息。如何删除我不明白的列,但是当我运行删除行的脚本时,信息仍然存在,只删除了样式。如何正确执行此类操作?


class DownloadRfiExcelFile(APIView):

    """

    Download rfi excel file to user

    """

    def get(self, request, format=None, **kwargs):

        file = default_storage.url('test.xlsx')

        wb = load_workbook(filename=file)


        response = HttpResponse(content_type='application/vnd.ms-excel')

        response['Content-Disposition'] = 'attachment; filename="test.xlsx"'

        sheet = wb["RT"]

        for rowNum in range(3, 1114):

                sheet.delete_rows(rowNum)

        for col in sheet.iter_cols():

            for cell in col:

                # delete from U to AA row

        wb.save(response)

        return response


繁华开满天机
浏览 94回答 1
1回答

慕慕森

要删除rows并columns仅使用:sheet.delete_rows({first_row}, {amount})sheet.delete_cols({first_col}, {amount})因此,请阅读openpyxl 文档中的更多信息。请注意,列表示为整数A == 1, B ==2,依此类推。import stringdef col2num(col):    # Utility function to convert column letters to numbers    num = 0    for c in col:        if c in string.ascii_letters:            num = num * 26 + (ord(c.upper()) - ord('A')) + 1    return num# Setting initial values for deletionstarting_row = 4starting_col = col2num('U')last_col = col2num('AA')# Deleting rows and columnssheet.delete_rows(starting_row, sheet.max_row-starting_row)sheet.delete_cols(starting_col, last_col-starting_col)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python