简体   繁体   English

使用 Styleframe 从 Excel 中提取单个单元格的 styles

[英]Using Styleframe to pull styles of individual cells from Excel

I'm trying to write a script that merges two excel files together.我正在尝试编写一个将两个 excel 文件合并在一起的脚本。 One has been has been hand processed and has a bunch custom formatting done to it, and the other is an auto-generated file.一个已经过手工处理,并对其进行了一系列自定义格式化,另一个是自动生成的文件。 Doing the merge in pandas is simple enough, but preserving the formatting is proving troublesome.在 pandas 中进行合并很简单,但事实证明保留格式很麻烦。 I found the styleframe library, which seems like it should simplify what I'm trying to do, as it can import style info in addition to the raw data.我找到了 styleframe 库,它似乎应该简化我正在尝试做的事情,因为除了原始数据之外,它还可以导入样式信息。 However, I'm having problems actually implementing the code.但是,我在实际实现代码时遇到了问题。

My questions is this: how can I pull style information from each individual cell in the excel and then apply that to my merged dataframe?我的问题是:如何从 excel 中的每个单独单元格中提取样式信息,然后将其应用于我合并的 dataframe? Note that the data is not formatted consistently across columns or rows, so I don't think I can apply styles in this manner.请注意,数据在列或行之间的格式不一致,所以我认为我不能以这种方式应用 styles。 Here's the relevant portion of my code:这是我的代码的相关部分:

#iterate thorough all cells of merged dataframe
for rownum, row in output_df.iterrows():
    for  column, value in row.iteritems(): 
        filename = row['File Name']

        cur_style = orig_excel.loc[orig_excel['File Name'] == filename, column][0].style #pulls the style of relevant cell in the original excel document
        target_style = output_df.loc[output_df['File Name'] == filename, column][0].style #style of the cell in the merged dataframe
        target_style = cur_style #set style in current output_df cell to match original excel file style

This code runs (slowly) but it doesn't seem to actually apply any styling to the output styleframe此代码运行(缓慢),但它似乎实际上并未对 output 样式框架应用任何样式

Looking through the documentation, I don't really see a method for applying styles at an individual styleframe container level--everything is geared towards doing it as a row or column.浏览文档,我并没有真正看到在单个样式框架容器级别应用 styles 的方法——一切都是为了将其作为行或列进行。 It also seems like you need to use a styler object to set the style.似乎您还需要使用样式器 object 来设置样式。

Figured it out.弄清楚了。 I rejiggered my dataframe so that I could just us a .at instead of a .loc lookup.我重新调整了我的 dataframe 以便我可以只使用.at而不是.loc查找。 This, coupled with the apply_style_by_indexes method got me where I needed to be:这一点,再加上apply_style_by_indexes方法让我到达了我需要的地方:

for index, row in orig_excel.iterrows():
    for  column, value in row.iteritems(): 
        index_num = output_df.index.get_loc(index)

        #Pull style to copy to new df
        cur_style = orig_excel.at[index, column].style 
      
        #Apply original style to new df
        output_df.apply_style_by_indexes(output_df.index[index_num], 
                                         cur_style, 
                                         cols_to_style = column)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM