【问题标题】:Need to make multiple files from a single excel file需要从一个excel文件制作多个文件
【发布时间】:2009-08-04 03:01:19
【问题描述】:

我有一个 excel 文件。有很多列。我需要使用这个制作多个文件

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

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

  1. 我有一个包含列数和行数的 Excel 文件。这些列被命名为

alt text http://img44.imageshack.us/img44/3397/84200961244pm.png

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

包含所有行的 A 到 O 列。第二个将有 A 到 N + P(这不会有 O 列)和类似的其他 2。会有很多列有 2,我将不得不制作一个文件,其中包含所有包含 O 和 1 的列以及每个 2一次。即 1st 2 然后 2nd 2 依此类推。

【问题讨论】:

  • 如果您能更清楚地了解您的 excel 文件的结构以及您希望输出的样子,我相信我们可以给您一些更具体的建议。
  • 哇,这有点含糊。几乎无法理解。
  • 更好,但仍然很混乱。文件 1 有 A 到 O 列,文件 2 有 A 到 N 列加上 P 列,但我不确定文件 3 和 4 应该有哪些列。一个猜测是文件 3 有 A 到 N 列加上 Q 列,文件 4 有 A 到 N 列加上 R 列。我也有一种感觉,你想生成额外的文件,但我不知道应该进入什么数据那些文件。

标签: python perl


【解决方案1】:

您可以使用Spreadsheet::ParseExcel 阅读电子表格。不幸的是,我只能为您提供帮助,因为坦率地说,您对问题的描述毫无意义。

【讨论】:

    【解决方案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])
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        在 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?

        【讨论】:

          【解决方案5】:

          正如其他人所评论的那样,您的问题几乎完全无法理解。根据您描述问题的困难,您可能想看看 this post.

          这里有些人建议将您的文件保存为 CSV。将文件保存为 CSV 文件将大大简化解析工作,但它会使与 excel 格式之间的转换成为手动过程。如果您要处理的文件数量很少,这可能是可以接受的。如果你有数百个,它不会工作得那么好。

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

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

          【讨论】:

            【解决方案6】:

            我认为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? :)

            【讨论】:

            【解决方案7】:

            您可以使用 Visual Basic for Applications 来loop over the cells 然后save to a text file

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-03-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-02-01
              • 1970-01-01
              相关资源
              最近更新 更多