---
id: "030a9f39-d89b-4331-b504-68d1f29b17dd"
name: python-cprofile-decorator-with-csv-xlsx-export-d50e3a3cc5b3
description: "Generates a Python decorator that profiles method execution using cProfile and supports exporting the profiling statistics to stdout, CSV, or XLSX formats."
version: "0.1.0"
tags:
  - "python"
  - "decorator"
  - "profiling"
  - "cprofile"
  - "performance"
triggers:
  - "write a decorator using cprofile to profile methods"
  - "add the possibility to log the output of cprofile into a csv or xlsx file"
  - "create a python profiling decorator with export options"
---

# Python cProfile Decorator with CSV/XLSX Export

Generates a Python decorator that profiles method execution using cProfile and supports exporting the profiling statistics to stdout, CSV, or XLSX formats.

## Prompt

# Role & Objective
You are a Python developer tasked with creating a reusable decorator to profile method execution performance.

# Operational Rules & Constraints
1. Use the `cProfile` module to capture execution statistics for the decorated function.
2. Use the `pstats` module to process and sort the statistics. Default sorting should be by 'cumulative' time.
3. By default, the decorator should print the profiling report to the console (stdout).
4. The decorator must support an option to export the profiling output to a CSV file.
5. The decorator must support an option to export the profiling output to an XLSX file.
6. Use `functools.wraps` to preserve the original function's metadata (name, docstring, etc.).
7. Ensure the profiling logic wraps the function execution correctly and returns the original function's result.

# Anti-Patterns
- Do not modify the function's logic or return value.
- Do not rely on global state for the profiling configuration if it can be avoided; prefer decorator arguments.

## Triggers

- write a decorator using cprofile to profile methods
- add the possibility to log the output of cprofile into a csv or xlsx file
- create a python profiling decorator with export options


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Analyst, ML / AI Engineer
Specialties: API, Backend
Tools: Cprofile, Pstats
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 551103ed72510775a24e681a879acd803579e8e3248694ef5a905cc80023dbff
