quicktool/excel/convert_md copy.py

40 lines
1.8 KiB
Python

import openpyxl
import sys
def excel_to_markdown(excel_file, output_file, selected_columns):
# Load the workbook
wb = openpyxl.load_workbook(excel_file)
sheet = wb.active
# Get the header row
header = [cell.value for cell in sheet[1]]
# Find the indices of the selected columns
column_indices = [header.index(col) for col in selected_columns if col in header]
# Open the output file
with open(output_file, 'w', encoding='utf-8') as f:
# Write header row
f.write('| ' + ' | '.join(selected_columns) + ' |\n')
f.write('| ' + ' | '.join(['---' for _ in selected_columns]) + ' |\n')
# Iterate through rows
for row in sheet.iter_rows(min_row=2, values_only=True):
# Extract only the selected columns in the specified order
selected_values = [str(row[header.index(col)]) if row[header.index(col)] is not None else '' for col in selected_columns if col in header]
f.write('| ' + ' | '.join(selected_values) + ' |\n')
if __name__ == '__main__':
if len(sys.argv) < 3:
print("Usage: python convert_md.py <input_excel_file> <output_markdown_file> <column1> <column2> ...")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
# selected_columns = sys.argv[3:]
#selected_columns = ['客户名称(必填)','商机名称(必填)', 'Sales stage', '预测类型','Timing 风险','预估 ACV', '预计成交日期','当前详细状态及Close节奏']
selected_columns = ['客户名称(必填)','商机名称(必填)', 'Sales stage','预估 ACV', '预计成交日期','当前详细状态及Close节奏']
excel_to_markdown(input_file, output_file, selected_columns)
print(f"Conversion complete. Markdown file saved as {output_file}")