如何通过使用openpyxl在Excel工作表中插入复制的列/行

时间:2019-06-14 08:58:35

标签: python openpyxl

我有一个Excel工作表。它需要实现以下逻辑:

复制该列并将其粘贴到该列的所需位置,作为新列或新列。与字符串相同。

复制的列可能包含数据,不一定为空。

我需要一个接受四个参数作为输入的函数:

  1. Excel工作表
  2. 复制的列/行的索引;
  3. 在其中插入复制的列/行的索引;
  4. 插入的列数/行数;

实现此逻辑的函数应类似于下面的伪代码(我认为是这样):

def insert_copied_column(ws: Worksheet, 
                         index_from_column: int, 
                         index_to_column:int, 
                         number_of_inserted_cols: int):

        copy_need_col(ws, index_from_column) # one column

        # It is the insertion of new columns, 
        # NOT the replacement of existing columns 
        # with the data of the copied column

        insert_copied_col(ws, index_to_column, number_of_inserted_cols)

        return ws # modified_worksheet

PS:

我认为行的功能类似于列的功能。

更新(针对查理·克拉克):

def insert_copied_column(ws: Worksheet, 
                         index_from_column: int, 
                         index_to_column:int, 
                         number_of_inserted_cols: int):
    # Inserting blank cols
    ws.insert_columns(idx=index_to_column, amount=number_of_inserted_cols)

    # How to copy need column (ind=index_from_column) and paste its in new blank columns?

0 个答案:

没有答案