【问题标题】:openpyxl - Compiling identical sheets to new sheetopenpyxl - 将相同的工作表编译为新工作表
【发布时间】:2018-10-26 17:16:54
【问题描述】:

我有一个 Excel 工作簿,其中包含相同样式的表格(相同的列数、相同的数据类型)的多个选项卡。我想将每个表合并(追加?堆栈?外连接?无论哪个!)到一个新工作表中,并满足以下要求:

  • 我不想重复标题。
  • 我不知道每个现有选项卡中的行数。
  • 我想知道每一行来自哪个选项卡。

到目前为止,我的概念过程如下所示:

  1. 在工作簿中加载
  2. 获取工作表列表
  3. 循环遍历我创建的工作表列表。
  4. 添加一个新列 A,其中包含每个单元格中的工作表名称。
  5. 添加新的空白工作表
  6. 循环遍历我再次创建的工作表列表
  7. 将数据附加到我的新工作表中,不包括第一次迭代后的第一行。
  8. 保存工作簿

这是我目前的代码:

def combine(path):
    # Load in workbook
    wb = xl.load_workbook(filename=path)

    # Get a list of sheets
    ws_list = wb.sheetnames

    # Loop over the list of sheets I created
    for i, ws in enumerate(ws_list):
        # Add a new column A
        wb[ws].insert_cols(idx=0)  
        for column in wb[ws]['A{0}:A{1}'.format(wb[ws].min_row, wb[ws].max_row)]:
            for cell in column:
                # that contains the name of the sheet in every cell
                cell.value = str(ws)

    # Add a new empty sheet
    wb.create_sheet(title=u'COMBINED',index=0)
    # Loop over the list of sheets I created againv
    for i, ws in enumerate(ws_list):
        # Append the data (currently should copy headers as well)
        wb['COMBINED'].append(wb[ws].rows)

    # Save workbook
    wb.save(path) 

这是我得到的错误

请注意,“Fall 03”是第一个选项卡名称,是添加到第一个选项卡第一列的文本。因此,我假设该过程正在完成第 5 步。

Traceback (most recent call last):
  File "test.py", line 194, in <module>
    main()
  File "test.py", line 188, in main
    combine(excel_path)
  File "test.py", line 166, in combine
    wb['COMBINED'].append(wb[ws].rows)                          <- my script
  File "C:\Python27\ArcGISx6410.5\lib\site-packages\openpyxl\worksheet\worksheet.py", line 777, in append
    cell = Cell(self, row=row_idx, col_idx=col_idx, value=content)
  File "C:\Python27\ArcGISx6410.5\lib\site-packages\openpyxl\cell\cell.py", line 115, in __init__
    self.value = value
  File "C:\Python27\ArcGISx6410.5\lib\site-packages\openpyxl\cell\cell.py", line 294, in value
    self._bind_value(value)
  File "C:\Python27\ArcGISx6410.5\lib\site-packages\openpyxl\cell\cell.py", line 207, in _bind_value
    raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert (<Cell u'Fall 03'.A1>, <Cell u'Fall 03'.B1>, <Cell u'Fall 03'.C1>, <Cell u'Fall 03'.D1>, <Cell u'Fall 03'.E1>, <Cell u'Fall 03'.F1>, <Cell u'Fall 03'.G1>, <Cell u'Fall 03'.H1>, <Cell u'Fall 03'.I1>, <Cell u'Fall 03'.J1>, <Cell u'Fall 03'.K1>, <Cell u'Fall 03'.L1>, <Cell u'Fall 03'.M1>, <Cell u'Fall 03'.N1>, <Cell u'Fall 03'.O1>, <Cell u'Fall 03'.P1>, <Cell u'Fall 03'.Q1>, <Cell u'Fall 03'.R1>, <Cell u'Fall 03'.S1>, <Cell u'Fall 03'.T1>, <Cell u'Fall 03'.U1>, <Cell u'Fall 03'.V1>, <Cell u'Fall 03'.W1>, <Cell u'Fall 03'.X1>, <Cell u'Fall 03'.Y1>, <Cell u'Fall 03'.Z1>, <Cell u'Fall 03'.AA1>, <Cell u'Fall 03'.AB1>, <Cell u'Fall 03'.AC1>, <Cell u'Fall 03'.AD1>, <Cell u'Fall 03'.AE1>, <Cell u'Fall 03'.AF1>, <Cell u'Fall 03'.AG1>, <Cell u'Fall 03'.AH1>, <Cell u'Fall 03'.AI1>, <Cell u'Fall 03'.AJ1>, <Cell u'Fall 03'.AK1>, <Cell u'Fall 03'.AL1>) to Excel

如果我能挑剔的话

到目前为止,我一直在尝试使用 openpyxl,因为我知道它安装在我需要使用的所有机器上。我还可以使用标准 ArcGIS 10.5 ArcPy 发行版中包含的任何模块。

我查看了这些解决方案,但似乎没有一个有效:

  1. This one seems to use the '.rows' iterable incorrectly
  2. This one worn't work because I don't know the number of rows in the existing tables.
  3. This one won't work because they are asking to copy between tables, which is a non-trivial distiction for some reason.

【问题讨论】:

    标签: python excel python-2.7 openpyxl


    【解决方案1】:

    你不能这样做wb['COMBINED'].append(wb[ws].rows)。您只能附加一系列简单的项目,例如数字或字符串,并且您尝试传递包含 Cell 对象的元组序列。 for row in ws1.values: ws.append(row) 是可能的。

    您的代码也过于复杂:您可以简单地遍历工作簿中的工作表:for ws in wb 并使用 ws.iter_rows()ws.iter_cols() 进行参数访问。

    【讨论】:

    • 让我看看我是否理解:听起来“行”是我可以使用ws.append 方法添加的最基本级别。真的吗?而且我不知道for ws in wb: 使用的默认__iter__ 行为,这真的很有帮助。
    • 不,标准 Python 类型的序列是您可以附加的全部。
    猜你喜欢
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多