将大型Excel文件拆分为多个文件
我有一个非常大的Excel文件。在几行之间有“------------”这个字符串。我想把从一个“------------”到另一个“------------”之间的内容分开,并用“------------”旁边的单元格里的文字给这些文件命名。请帮我实现这个。
1 个回答
1
我会使用类似于这个网站的东西:http://www.python-excel.org/。
pip install xlrd xlwt
xlrd - 用来读取Excel文件
xlwt - 用来写入Excel文件
然后我会尝试做一些这样的事情:
import xlrd
import xlwt
def write_rows(batch, filename):
current_batch_xls = xlwt.Workbook(encoding='utf-8')
first_sheet = current_batch_xls.add_sheet(filename + ' sheet')
for row_number, row in enumerate(batch):
for cell_number, cell in enumerate(row):
first_sheet.write(row_number, cell_number, cell.value, style=cell.xf_index)
current_batch_xls.save(filename)
FILENAME='big-excel-spreadsheet.xls'
DELIMITER='------------'
big_spreadsheet = xlrd.open_workbook(FILENAME)
# assuming you have only one sheet
sheet = big_spreadsheet.sheet_by_index(0)
current_batch_of_rows = []
for row in xrange(2, sheet.nrows):
if row.cell(row, 0) == DELIMITER:
write_rows(current_batch_of_rows, filename=row.cell(row, 1))
current_batch_of_rows = []
continue
current_batch_of_rows.append(sheet.row(row))
这个还没有测试过。关于 xlrd
和 xlwt
的文档似乎很糟糕。