view xlsx2tsv.py @ 0:a21347be425d draft default tip

planemo upload for repository https://github.com/Helmholtz-UFZ/galaxy-tools/tree/main/tools/xls2tsv commit 2e75a14496fa80104e76b307289c58b9b7013ae1
author ufz
date Mon, 16 Dec 2024 20:56:29 +0000
parents
children
line wrap: on
line source

import argparse

import pandas as pd


def convert_xlsx_to_tsv(input_file, sheet_name, output):
    try:
        # Read the specified sheet and convert them to tsv
        df = pd.read_excel(input_file, sheet_name=sheet_name)
        df.to_csv(output, sep='\t', index=False)
        print(f"Extracted sheet '{sheet_name}' from {input_file}")

    except Exception as e:
        print(f"Failed to convert sheet '{sheet_name}' from {input_file}: {e}")


def main():
    parser = argparse.ArgumentParser(description="Convert specific sheets from a single .xlsx file to .tsv format in the same directory.")
    parser.add_argument("--input-file", type=str, required=True, help="Path to the input .xlsx file.")
    parser.add_argument("--sheet-names", type=str, required=True, help="Comma-separated list of sheet names to convert.")
    parser.add_argument("--output", type=str, default="extracted_sheet.tsv", required=False, help="Suffix for the tsv file")
    args = parser.parse_args()

    # Convert sheet names from str to list
    sheet_names = args.sheet_names

    # Call the conversion function with the provided arguments
    convert_xlsx_to_tsv(args.input_file, sheet_names, args.output)


if __name__ == "__main__":
    main()