# How do I export a report as a CSV file using python-gvm?

**URL:** <https://forum.greenbone.net/t/how-do-i-export-a-report-as-a-csv-file-using-python-gvm/17516>\
**Category:** How To\
**Created:** [April 20, 2024, 7:39am UTC](https://forum.greenbone.net/t/how-do-i-export-a-report-as-a-csv-file-using-python-gvm/17516 "2024-04-20T07:39:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiljoy001](https://forum.greenbone.net/letter_avatar/kiljoy001/32/6_449c533f586939b6e5f246632a46421f.png) [@kiljoy001](https://forum.greenbone.net/u/kiljoy001)\
**Post date:** [April 20, 2024, 7:39am UTC](https://forum.greenbone.net/t/how-do-i-export-a-report-as-a-csv-file-using-python-gvm/17516/1 "2024-04-20T07:39:23Z")

</div>

I’ve attempted to write some code to export the scan reports as a csv file:

```auto
from gvm.connections import UnixSocketConnection
from gvm.protocols.latest import Gmp, ReportFormatType
import xml.etree.ElementTree as Et

USERNAME = 'user'
PASSWORD = 'blah'
CSV_FORMAT = ReportFormatType.CSV_RESULTS
CONNECTION = UnixSocketConnection()

def process_all_reports(xml_data: str) -> list:
    try:
        xml_root = Et.fromstring(xml_data)
        return list(set(report_data.get('id') for report_data in xml_root.findall('report')))
    except Et.ParseError as e:
        print(f"Parsing failed!\nDetails: {e}")
        return []

def get_csv_report() -> None:
    """
    Logs into GVM to pull reports and saves them as CSV files.
    :return: None
    """
    try:
        with Gmp(CONNECTION) as gmp:
            gmp.authenticate(username=USERNAME, password=PASSWORD)
            # logger.info("Authentication successful.")
            print("Auth Success")

            # Get all reports
            raw_reports = gmp.get_reports()
            report_id_list = process_all_reports(raw_reports)
            print(f"got {len(report_id_list)} reports.\nReports: {report_id_list}")
            # logger.info(f"Gathered {len(reports)} reports.")

            # download all reports in csv format
            for report_id in report_id_list:
                try:
                    report_data = gmp.get_report(report_id, ignore_pagination=True, report_format_id=CSV_FORMAT)
                    try:
                        with open(f'report_{report_id}.csv', 'w') as report_file:
                            print(f"Data check:{report_id}: {report_data}")
                            report_file.write(report_data)
                            report_file.close()
                            print(f"saved report {report_id} as CSV file")
                    except Exception as e:
                        print(f"Error attempting to write files!\nDetails: {e}")
                    # logger.info(f"saved report {report_id} as CSV file")
                except Exception as e:
                    print(f"Saving report {report_id} failed!\nDetails: {e}")
                    # logger.exception(f"Saving report {report_id} failed!\nDetails: {e}")
                    pass
    except Exception as e:
        print(f"An error occurred when starting to extract reports!\nDetails: {e}")
        # logger.exception(f"An error occurred when starting to extract reports!\nDetails: {e}")
        pass

# Call the function to test
get_csv_report()

```

While there is another issue with the code not saving the files, my most pressing issue is that the output of the get\_report method seems to produce base64 inside of a xml tree. Unfortunately, there doesn’t appear to be any tags wrapping the base64 string, so I am at a loss how I would be able to extract the string to transform it into something that could be saved as a csv file.  
How can I accomplish this? Are there any docs that give a short how to?

---

<div class="post-metadata">

**Author:** ![system](https://forum.greenbone.net/uploads/default/original/2X/4/4660e797edfaea797a8c9965246072ea26732804.png) [@system](https://forum.greenbone.net/u/system)\
**Post date:** [April 7, 2025, 10:21am UTC](https://forum.greenbone.net/t/how-do-i-export-a-report-as-a-csv-file-using-python-gvm/17516/2 "2025-04-07T10:21:44Z")

</div>

Closed on April 7, 2025, 10:21am UTC


