Mercurial > repos > ufz > xlsx2tsv
comparison 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 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:a21347be425d |
|---|---|
| 1 import argparse | |
| 2 | |
| 3 import pandas as pd | |
| 4 | |
| 5 | |
| 6 def convert_xlsx_to_tsv(input_file, sheet_name, output): | |
| 7 try: | |
| 8 # Read the specified sheet and convert them to tsv | |
| 9 df = pd.read_excel(input_file, sheet_name=sheet_name) | |
| 10 df.to_csv(output, sep='\t', index=False) | |
| 11 print(f"Extracted sheet '{sheet_name}' from {input_file}") | |
| 12 | |
| 13 except Exception as e: | |
| 14 print(f"Failed to convert sheet '{sheet_name}' from {input_file}: {e}") | |
| 15 | |
| 16 | |
| 17 def main(): | |
| 18 parser = argparse.ArgumentParser(description="Convert specific sheets from a single .xlsx file to .tsv format in the same directory.") | |
| 19 parser.add_argument("--input-file", type=str, required=True, help="Path to the input .xlsx file.") | |
| 20 parser.add_argument("--sheet-names", type=str, required=True, help="Comma-separated list of sheet names to convert.") | |
| 21 parser.add_argument("--output", type=str, default="extracted_sheet.tsv", required=False, help="Suffix for the tsv file") | |
| 22 args = parser.parse_args() | |
| 23 | |
| 24 # Convert sheet names from str to list | |
| 25 sheet_names = args.sheet_names | |
| 26 | |
| 27 # Call the conversion function with the provided arguments | |
| 28 convert_xlsx_to_tsv(args.input_file, sheet_names, args.output) | |
| 29 | |
| 30 | |
| 31 if __name__ == "__main__": | |
| 32 main() |
