如何使用python在Excel中折叠行?

我目前从我的database拉一个csv列表。 然后,我将这些数据插入到数据dataframe ,然后将数据插入到excel文件中。 每个数据元素都是我想折叠具有类似顶级节点的行的节点的集合。 我现在的问题是我可以find的唯一资源是xlsxwriter ,但是我需要将数据写入已经存在的excel文件。 有没有人有成功使用不同的方法?

xlrd ,我使用了模块xlrdxlutils 。 如果你需要更高级的控制来编写你的Excel单元格,还有xlwt

要写入一个现有的文件,它看起来像这样:

 import xlrd from xlutils.copy import copy as xlcopy read_book = xlrd.open_workbook('filepath/to/file.xlsx') read_sheet = read_book.sheet_by_index(0) write_book = xlcopy(read_book) write_sheet = write_book.get_sheet(0) 

您可以通过执行read_sheet来读取文件中的现有内容:

 contents = read_sheet.cell(row_index, col_index).value 

您可以通过下列方式将新内容写入write_sheet:

 write_sheet.write(row_index, col_index, 'Cell contents') 

然后最后保存回原始文件。