Py学习  »  Python

Python中的openpyxl delete_rows函数用于断开合并单元格

Otsuki Takaya • 6 年前 • 3087 次点击  

我在Python3中使用openpyxl 2.6.0(与Django一起)。

我想操作我的excel skelton工作表(合并了一些单元格)来删除一行。但删除行后,合并的单元格不再合并。

即使删除了某些行,我也要保留合并的单元格。

代码和excel示例如下。

def excelsample(request):

    wb = openpyxl.load_workbook(os.path.join(settings.BASE_DIR, 'static', 'excel', 'sample.xlsx'))

    sheet = wb['Sheet1']
    sheet.delete_rows(6) # delete 6th row

    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="{fn}"'.format(fn=urllib.parse.quote("excelsample.xlsx"))

    wb.save(response)

    return response

之前:

enter image description here

enter image description here

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54632
文章 [ 1 ]  |  最新文章 6 年前