简体   繁体   English

使用 openpyxl 在 excel 下方附加数据框时,我的 excel 表中的切片器被破坏

[英]Slicer in my excel sheet get destroyed while appending dataframe below excel using openpyxl

I am working with pandas and openpyxl.我正在使用熊猫和 openpyxl。

INPUT FILES输入文件

I have total three input excel files in my program.我的程序中共有三个输入 excel 文件。 With the help of dataframes I am processing input excel files and getting a final dataframe after processing.在数据框的帮助下,我正在处理输入的 excel 文件并在处理后获得最终的数据框。

OUTPUT输出

After getting final dataframe in my program, I am writing this dataframe below an existing excel file with the help of openpyxl.在我的程序中获得最终数据框后,我在 openpyxl 的帮助下将此数据框写入现有的 excel 文件下。 This excel file contains many worksheets.这个 excel 文件包含许多工作表。 Some worksheets in this excel file also contains pivot table and slicer.此 excel 文件中的某些工作表还包含数据透视表和切片器。 Dataframe is successfully appending below excel file.数据框已成功附加到 excel 文件下方。

**But problem is Slicer of my pivot is getting destroyed while writing dataframe below excel file.**I am getting following warning during execution of my program **但问题是在 excel 文件下写入数据帧时,我的枢轴切片器被破坏。**我在执行程序期间收到以下警告

C:\Users\Desktop\PycharmProjects\MyProject\venv\lib\site-packages\openpyxl\worksheet\_reader.py:292: UserWarning: Slicer List extension is not supported and will be removed
      warn(msg)
  1. I am using following method to append dataframe below an existing excel sheet with the help of openpyxl-我正在使用以下方法在 openpyxl 的帮助下将数据框附加到现有的 Excel 工作表下方-

    HELPER FUNCTION TO APPEND DATAFRAME BELOW EXCEL FILE在 Excel 文件下方附加数据框的帮助函数

    def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None, truncate_sheet=False, **to_excel_kwargs): """ Append a DataFrame [df] to existing Excel file [filename] into [sheet_name] Sheet. If [filename] doesn't exist, then this function will create it. def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None, truncate_sheet=False, **to_excel_kwargs): """ 将 DataFrame [df] 附加到现有 Excel 文件 [filename] 到 [sheet_name] 表中。如果 [ filename] 不存在,则此函数将创建它。

     Parameters: filename : File path or existing ExcelWriter (Example: '/path/to/file.xlsx') df : dataframe to save to workbook sheet_name : Name of sheet which will contain DataFrame. (default: 'Sheet1') startrow : upper left cell row to dump data frame. Per default (startrow=None) calculate the last row in the existing DF and write to the next row... truncate_sheet : truncate (remove and recreate) [sheet_name] before writing DataFrame to Excel file to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()` [can be dictionary] Returns: None """ from openpyxl import load_workbook import pandas as pd # ignore [engine] parameter if it was passed if 'engine' in to_excel_kwargs: to_excel_kwargs.pop('engine') writer = pd.ExcelWriter(filename, engine='openpyxl', index=False, data_only = 'True') # Python 2.x: define [FileNotFoundError] exception if it doesn't exist try: FileNotFoundError except NameError: FileNotFoundError = IOError try: # try to open an existing workbook writer.book = load_workbook(filename) # get the last row in the existing Excel sheet # if it was not specified explicitly if startrow is None and sheet_name in writer.book.sheetnames: startrow = writer.book[sheet_name].max_row # truncate sheet if truncate_sheet and sheet_name in writer.book.sheetnames: # index of [sheet_name] sheet idx = writer.book.sheetnames.index(sheet_name) # remove [sheet_name] writer.book.remove(writer.book.worksheets[idx]) # create an empty sheet [sheet_name] using old index writer.book.create_sheet(sheet_name, idx) # copy existing sheets writer.sheets = {ws.title:ws for ws in writer.book.worksheets} except FileNotFoundError: # file does not exist yet, we will create it pass if startrow is None: startrow = 1 # write out the new sheet df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs) # save the workbook writer.save()
  2. Following line is used to call above helper function以下行用于调用上面的辅助函数

    path_of_existing_excel_file = C:\\Users\\Desktop\\MyExcel.xlsx append_df_to_excel(path_of_existing_excel_file, df1, sheet_name='MY-DATA',index = False ) path_of_existing_excel_file = C:\\Users\\Desktop\\MyExcel.xlsx append_df_to_excel(path_of_existing_excel_file, df1, sheet_name='MY-DATA',index = False )

3.I am able to obtain desired output.The only Problem is pivot table in excel is getting destroyed. 3.我能够获得所需的输出。唯一的问题是excel中的数据透视表被破坏了。 All the sheets in my excel file which contains pivot table is losing information我的 excel 文件中包含数据透视表的所有工作表都丢失了信息

Here my output is useless because I lost all the information related to my pivot table.这里我的输出没用,因为我丢失了与数据透视表相关的所有信息。

Hope I am clear.希望我清楚。 Now I want to ask the way by which my pivot table will not destroy ?现在我想问一下我的数据透视表不会破坏的方式? How to do so that sheets which contains pivot table will not be affected by openpyxl?如何使包含数据透视表的工作表不受 openpyxl 的影响? Or How can write dataframe below excel sheet so that my pivot table will not getting affected?或者如何在 excel 表下方写入数据框,以便我的数据透视表不会受到影响? Any of answers to above question will help me以上问题的任何答案都会对我有所帮助

I have tried many solutions.But There is no solution available with openpyxl.我已经尝试了很多解决方案。但是 openpyxl 没有可用的解决方案。

So this problem can be solved by using xlwings library which is very efficient for data processing所以这个问题可以通过使用xlwings库来解决,它对数据处理非常有效

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

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