Excel文件未正确保存

时间:2020-06-07 22:48:43

标签: python excel pandas

我面临一个奇怪的问题,不确定如何解决。这里需要帮助。

从WEB HTML表获取数据并将其保存到工作表名称为“ Test”的“ ExcelTest01”文件中。我的Excel工作簿有多个工作表,并且每个工作表公式均已应用。

col_count=len(driver.find_elements_by_xpath("/html/body/form/table/tbody/tr[8]/td/table/tbody/tr[1]/th"))
row_count=len(driver.find_elements_by_xpath("/html/body/form/table/tbody/tr[8]/td/table/tbody/tr"))
path="C:\\python\\ExcelTest01.xlsx"
work_book=openpyxl.load_workbook(path)
work_book.template = False
work_sheet=work_book['Test01']
print("Excel writing is in progress")
for r in range(2,row_count+1):
    for c in range(1,col_count+1):
        table_data=driver.find_element_by_xpath(
            "/html/body/form/table/tbody/tr[8]/td/table/tbody/tr["+str(r)+"]/td["+str(c)+"]").text
        work_sheet.cell(row=r,column=c).value=table_data
        work_book.save(path)
        work_book.close()

从excel ExcelTest01中选择具有列名“ Value” = No的行,然后复制到其他excelExcelExcel02

下面的脚本仅将列名“ ExcelTest01”写入“ ExcelTest02”。但是,如果我手动保存excel工作簿“ ExcelTest01”,则下面的代码可以正常工作,这意味着在脚本下面的预期写入仅将“ ExcelTest0” 1的列名称写入“ ExcelTest02”。但是,如果我手动保存excel工作簿“ ExcelTest01”,则下面的代码可以正常工作,意味着可以按预期进行写入。

  print("Apply filter and copying to other excel")
    path1="C:/python/ExcelTest01.xlsx "
    df1 = pd.read_excel(path1,sheet_name="Test02")
#Worksheet Test02 in 'ExcelTest01.xlsx' has formula applied to get the required value from Sheet 'Test01'
    f1=df1['Value']
    f2=df1[f1 =='No']
    f2.to_excel(r'C:/python/ExcelTest02.xlsx,index = False)

0 个答案:

没有答案