Hi everyone,
I’m exporting a Report to Excel using the following code, and everything is working fine. However, in the exported file, the filters section shows fieldnames instead of labels. I want to display filter labels instead.
Here is my current code for exporting the report:
@frappe.whitelist()
def export_query():
"""Export from query reports"""
from frappe.desk.utils import get_csv_bytes, pop_csv_params, provide_binary_file
from frappe.utils.xlsxutils import make_xlsx
form_params = frappe._dict(frappe.local.form_dict)
csv_params = pop_csv_params(form_params)
clean_params(form_params)
parse_json(form_params)
report_name = form_params.report_name
frappe.permissions.can_export(
frappe.get_cached_value("Report", report_name, "ref_doctype"),
raise_exception=True,
)
file_format_type = form_params.file_format_type
custom_columns = frappe.parse_json(form_params.custom_columns or "[]")
include_indentation = form_params.include_indentation
visible_idx = form_params.visible_idx
if isinstance(visible_idx, str):
visible_idx = json.loads(visible_idx)
data = run(report_name, form_params.filters, custom_columns=custom_columns, are_default_filters=False)
data = frappe._dict(data)
if not data.columns:
frappe.respond_as_web_page(
_("No data to export"),
_("You can try changing the filters of your report."),
)
return
format_duration_fields(data)
# Fetch filters, but I'm getting fieldnames instead of labels
filters_list = []
for key, value in form_params.filters.items():
if isinstance(value, list):
value = ", ".join(value)
filters_list.append([key, value]) # I need labels instead of key (fieldname)
xlsx_data, column_widths = build_xlsx_data(data, visible_idx, include_indentation)
xlsx_data = [["Filters", ""]] + filters_list + [["", ""]] + xlsx_data
if file_format_type == "CSV":
content = get_csv_bytes(xlsx_data, csv_params)
file_extension = "csv"
elif file_format_type == "Excel":
file_extension = "xlsx"
content = make_xlsx(xlsx_data, "Query Report", column_widths=column_widths).getvalue()
provide_binary_file(report_name, file_extension, content)
Issue:
- The exported Excel file shows filter fieldnames instead of labels.
- I tried fetching labels using
frappe.get_doc("Report", report_name).filters
, but it returns an empty list. - How can I retrieve and use the correct filter labels?
Any help would be greatly appreciated!