需要从单个excel文件中创建多个文件

时间:2009-08-04 03:01:20

标签: python perl

我有一个excel文件。有很多专栏。我需要使用此

制作多个文件

例如:0 0 0 0 0 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2.所以这些是excel列,每列都有很多行。我需要一个文件,其中包含0 0 0 0 0 1 1 1 1 1 2然后第二个将只包含第二个没有0 0 0 0 0 1 1 1 1 1 2 ....类似其他。

谢谢大家的回复。为了简化问题:

  1. 我有一个excel文件,就像我提到的那样有列数和行数。列名为
  2. alt text http://img44.imageshack.us/img44/3397/84200961244pm.png

    现在我需要将此文件拆分为许多excel文件,第一个将具有

    包含所有行的A到O列。第二个将有A到N + P(这将没有列O)和类似的另外2.将有许多列2和我将必须使一个文件包含所有包含O和1和每个2的列一次。即第2个然后是第2个,依此类推。

7 个答案:

答案 0 :(得分:6)

您可以使用Spreadsheet::ParseExcel阅读电子表格。不幸的是,这就是我可以帮助你的所有内容,因为坦率地说,对你的问题的描述毫无意义。

答案 1 :(得分:2)

使用Python和xlrd& xlwt。见http://www.python-excel.org

以下脚本应该执行您想要的操作:

import xlrd, xlwt, sys

def raj_split(in_path, out_stem):
    in_book = xlrd.open_workbook(in_path)
    in_sheet = in_book.sheet_by_index(0)
    first_row = in_sheet.row_values(0)
    # find the rightmost 1 value in the first row
    split_pos = max(
        colx for colx, value in enumerate(first_row) if value == 1.0
        ) + 1
    out_book = xlwt.Workbook()
    out_sheet = out_book.add_sheet("Sheet1", cell_overwrite_ok=True)
    # copy the common cells
    for rowx in xrange(in_sheet.nrows):
        row_vals = in_sheet.row_values(rowx, end_colx=split_pos)
        for colx in xrange(split_pos):
            out_sheet.write(rowx, colx, row_vals[colx])
    out_num = 0
    # for each output file ...
    for out_col in range(split_pos, in_sheet.ncols):
        out_num += 1
        # ... overwrite the `split_pos` column
        for rowx, value in enumerate(in_sheet.col_values(colx=out_col)):
            out_sheet.write(rowx, split_pos, value)
        # ... and save the file.
        out_book.save("%s_%03d.xls" % (out_stem, out_num))

raj_split(*sys.argv[1:3])

答案 2 :(得分:1)

在python中,您可以使用xlrd将Excel电子表格读取为可以使用的数据。有关示例用法,请参阅README。然后,您可以使用xlwt创建新的电子表格。

答案 3 :(得分:1)

在Excel中,将文件另存为CSV。

在Python中,使用CSV阅读器模块阅读它(阅读python文档,搜索csv)

现在你说你有20列的行,你想把列1..10放在文件A中,列11..20放在文件B中,是吗?

打开2个csv编写器(让我们称之为A和B)

你会读到行:

表示csvreader中的行:     A.writerow(行[:10])     B.writerow(第[11:]行]

就是这样。

去这里: How can I merge fields in a CSV string using Python?

答案 4 :(得分:1)

正如其他人所评论的那样,你的问题几乎完全是不可理解的。根据您描述问题的难度,您可能需要查看 this post

有些人建议您将文件另存为CSV。将文件保存为CSV文件将大大简化解析它的工作,但它将使转换为excel格式和从excel格式转换为手动过程。如果您要处理少量文件,这可能是可以接受的。如果你有数百个,它将无法正常工作。

Spreadsheet::ParseExcelSpreadsheet::WriteExcel模块将帮助您以原生格式读取和编写电子表格文件。

Text::CSV_XS模块为perl提供了强大,快速的CSV解析器。

答案 5 :(得分:0)

我认为xlrd和xlwt模块是Python的基础。

# Read the first 5 rows and columns of an excel file
import xlrd # Import the package
book = xlrd.open_workbook("sample.xls") # Open an .xls file
sheet = book.sheet_by_index(0) # Get the first sheet
for row in range(5): # Loop for five times (five rows)
    # grab the current row
    rowValues = sheet.row_values(row, start_col=0, end_colx=4)
    # Do magic here, like printing
    import xlrd # Import the package
    print "%-10s | %-10s | %-10s | %-10s | %-10s" % tuple(rowValues)

现在,如果您想写回Excel文件......

import xlwt # Import the package
wbook = xlwt.Workbook() # Create a new workbook
sheet = wbook.add_sheet("Sample Sheet") # Create a sheet
data = "Sample data" # Something to write into the sheet
for rowx in range(5):
    # Loop through the first five rows
    for colx in range(5):
        # Loop through the first five columns
        # Write the data to rox, column
        sheet.write(rowx, colx, data)
# Save our workbook on the harddrive
wbook.save("myFile.xls")

我在该部分中广泛使用此方法来读取/写入Excel文件中的数据,以便在NetworkX中使用输入/输出模型。上面的例子来自我的博客文章,谈论冒险。

由于我是新用户,我只能发布一个链接。也许你可以谷歌xlwt? :)

答案 6 :(得分:-1)

您可以将Visual Basic for Applications用于loop over the cells,然后save to a text file

OR

将文件另存为逗号分隔值文件,并使用perl或python轻松解析这些行。 (拆分列的逗号,行的行尾字符)

相关问题