2014-08-30 135 views
-3

我有一个非常巨大的excel文件。在几行之间有“------------”这个字符串。我想将文件从“------------”分割为另一个“------------”,并将它们命名为“ - ” ----------“的下一个单元格。请帮助我做到这一点。将一个巨大的excel文件分割成多个文件

+0

你可以上传一个xls电子表格样本来测试吗?它会一直是“------------”旁边的单元吗? – 2014-08-30 22:02:51

+0

@MahmoudAbdelkader它将始终是“------------”旁边的单元 – user3783999 2014-08-30 22:03:41

+0

你在哪个平台上? – 2014-08-30 22:15:14

回答

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)) 

未经检验。对于xlrdxlwt,文档看起来非常糟糕。

相关问题