我正在为一个脚本创建函数,该脚本从一个api获取数据,该api将数据帧输出到excel工作表。我需要自动筛选(作为可选参数)。你知道吗
我差不多完成了,但是通过下面的解释不断得到一个属性错误“Worksheet”对象没有属性“autofilter”
这是我一直在尝试的代码
def generate_xls(df, file_name, sheet_name, column_names=None, auto_filter=True):
if column_names != None:
df.columns = column_names
print("appending the data in order...")
print("\nCreating excel-file")
writer = pd.ExcelWriter(file_name + '.xls')
df.to_excel(writer,sheet_name,index=False)
if auto_filter == True:
for worksheet in writer.sheets.values():
worksheet.autofilter(df.columns)
writer.save()
print("Excel-file generated.")
它没有自动过滤功能也能正常工作,但对我来说拥有它真的很重要,因为还有其他人可能需要它。任何建议都太好了!你知道吗
我认为您首先需要添加自动过滤器,假设您使用的是OpenPyXl:
当然,您可以省略
add_filter
和add_sort_conditions
,并将其留给用户进行配置。You will need to do simply:谢谢你的意见。我在writer对象中使用pandas和xlsxwriter引擎解决了这个问题:
相关问题 更多 >
编程相关推荐