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

问题描述 投票:0回答:1

我正在尝试使用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的理解 - 如果你使用一系列行突出显示速度,那么速度是安静的,而不是在另一行之后使用一行。

我不打算用像打字机等外部库这样做,

python dataframe
1个回答
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()

这应该加快你的循环(虽然可能没有线程那么多)。希望这有助于解决您的问题!

热门问题
推荐问题
最新问题