import openpyxl
import sys

def excel_to_markdown(excel_file, output_file):
    # Load the workbook
    wb = openpyxl.load_workbook(excel_file)
    sheet = wb.active

    # Open the output file
    with open(output_file, 'w', encoding='utf-8') as f:
        # Iterate through rows
        for row_index, row in enumerate(sheet.iter_rows(values_only=True)):
            # Write header row
            if row_index == 0:
                f.write('| ' + ' | '.join(str(cell) for cell in row) + ' |\n')
                f.write('| ' + ' | '.join(['---' for _ in row]) + ' |\n')
            # Write data rows
            else:
                f.write('| ' + ' | '.join(str(cell) if cell is not None else '' for cell in row) + ' |\n')

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: python convert_md.py <input_excel_file> <output_markdown_file>")
        sys.exit(1)

    input_file = sys.argv[1]
    output_file = sys.argv[2]

    excel_to_markdown(input_file, output_file)
    print(f"Conversion complete. Markdown file saved as {output_file}")
