Python-格式化Excel工作表太慢(逐行)

时间:2019-02-26 18:08:38

标签: python dataframe

我正在尝试使用python这样的功能来格式化excel表格,

def highlight_myrow_cells(sheetnumber, Sheetname ,dataFrame):
    Pre_Out_df_ncol = dataFrame.shape[1]
    RequiredCol_let = colnum_num_string(Pre_Out_df_ncol)

    #identifying the rows that needs to be highlighted
    arr = (dataFrame.select_dtypes(include=[bool])).eq(False).any(axis=1).values
    ReqRows = np.arange(1, len(dataFrame) + 1)[arr].tolist()
    #The ReqRows are a list of values something like [1,2,3,5,6,8,10]
    print("Highlighting the Sheet " + Sheetname + " in the output workbook")

    # Program is too slow over here---
    for i in range(len(ReqRows)):
        j = ReqRows[i] + 1
        xlwb1.sheets(sheetnumber).Range('A' + str(j) + ":" + RequiredCol_let + str(j)).Interior.ColorIndex = 6
    xlwb1.sheets(sheetnumber).Columns.AutoFit()

    for i in range(1, Emergency_df.shape[1]):
        j = i - 1
        RequiredCol_let = colnum_num_string(i)
        Required_Column_Name = (Emergency_df.columns[j])
        DateChecker1 = contains_word(Required_Column_Name, "Date", "of Death", "Day of Work")
        ResultChecker = Required_Column_Name.startswith("Result")
        if ResultChecker == False:
            if (DateChecker1 == True):
                xlwb1.sheets(sheetnumber).Range(Required_Column_Name + ":" + Required_Column_Name).NumberFormat = "m/d/yyyy"

程序根据逻辑突出显示行时速度太慢

据我从excel所了解的-如果突出显示使用一定范围的行,而不是一排又一排地突出显示,则速度非常好。

我不希望使用外部库(例如样式编写器等)来实现这一点,

1 个答案:

答案 0 :(得分:1)

因为您不能使用线程,所以我只是减少了执行每个循环所需的时间。我知道的方法看起来像:

    ReqRows += 1
    for i in range(len(ReqRows)):
        xlwb1.sheets(sheetnumber).Range('A{0}:{1}{0}'.format(i, RequiredCol_let)).Interior.ColorIndex = 6
    xlwb1.sheets(sheetnumber).Columns.AutoFit()

这应该可以加快循环速度(尽管可能不及线程速度快)。希望这可以帮助您解决问题!