Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何使用xlrd/xlwt/xlutils在excel文件的其他行之间插入行_Python_Excel - Fatal编程技术网

Python:如何使用xlrd/xlwt/xlutils在excel文件的其他行之间插入行

Python:如何使用xlrd/xlwt/xlutils在excel文件的其他行之间插入行,python,excel,Python,Excel,我试图打开一个excel文件,然后将数据插入第二个excel文件 行(将第一行下方的所有现有数据向下推一行)。我不想覆盖任何现有数据,只需添加新数据并将现有数据向下推一行即可 以下是我开始使用的一些代码: import xlrd import xlwt from xlutils.copy import copy def save_test_log(test_log_path, selected_save_path, test_type, date_n_time, tester):

我试图打开一个excel文件,然后将数据插入第二个excel文件 行(将第一行下方的所有现有数据向下推一行)。我不想覆盖任何现有数据,只需添加新数据并将现有数据向下推一行即可

以下是我开始使用的一些代码:

import xlrd  
import xlwt  
from xlutils.copy import copy 
def save_test_log(test_log_path, selected_save_path, test_type, date_n_time, tester):  
    rb = xlrd.open_workbook(test_log_path,formatting_info=True) 
    r_sheet = rb.sheet_by_index(0) # sets r_sheet to be the 1st sheet
    r = r_sheet.nrows # gets the number of rows in the 1st sheet.
    wb = copy(rb) # coppies contents of rb to a write file so we can edit it.
    sheet = wb.get_sheet(0) # select the 1st sheet in wb
这差不多是我能做到的了。我知道如何在工作表的末尾添加行 但我不知道如何在工作表中“插入”现有数据行

你有我想要的答案吗


谢谢,

这可以使用VBA轻松完成,VBA包含所有Excel对象库(范围、单元格、工作表)。我知道蟒蛇们对前景不屑一顾。但是,在微软移植Python之前,重写本机函数可能会过度劳累。此外,该包似乎不太广泛,无法执行插入行之类的细微差别。考虑编写宏并让Python使用它或将行迭代到CSV中并保存为工作表。谢谢您的输入。我想我会尝试使用xlrd将excel工作表读入列表,然后在列表中插入新的第二行和新数据。然后,我将使用xlwt使用列表列表中的数据覆盖旧的excel文件。不过你的方法听起来更干净,所以如果我的计划不起作用或者太慢,我可能会试试。谢谢你的帮助!此外,还将研究如何使用Python模块。您可以使用数据框(行和列的表格对象),而不是列表列表,这些数据框在其他分析过程中处理行插入。