指定されたEXCELファイル→Markdownファイル、Markdown→EXCELファイルに変換する。
作成する欄に〇が付いているテストデータファイルexcelファイルを読み込み
ExcelMarkdownConvert.py
import sys
import pandas as pd
from io import StringIO
import argparse
import re
import os
from datetime import datetime
from PyQt5.QtWidgets import (
QApplication
, QWidget
, QVBoxLayout
, QLabel
, QPushButton
, QFileDialog
, QMessageBox
, QLineEdit
, QHBoxLayout
)
# ===============================
# GUI部
# ===============================
class ExcelMarkDownConvert(QWidget):
# 初期処理
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
self.setWindowTitle('Excel<->Markdown変換ツール')
self.setFixedSize(600, 200)
layout = QVBoxLayout()
# 説明ラベル
instructions_label1 = QLabel('移行元データCSVのデータをチェックし、チェック結果CSVを作成します')
layout.addWidget(instructions_label1)
instructions_label2 = QLabel('※実行するには同じフォルダにチェック項目定義CSV(validate.csv)が必要です')
layout.addWidget(instructions_label2)
instructions_label3 = QLabel('移行元CSVファイルパスを入力(または参照)してください')
layout.addWidget(instructions_label3)
# User CSVファイル
validate_layout_1 = QHBoxLayout()
self.label1 = QLabel('変換Excelファイル名')
self.label1.setFixedWidth(100)
self.path_input_1 = QLineEdit()
self.browse_button1 = QPushButton('参照')
self.browse_button1.clicked.connect(self.browse_file_1)
self.mdconvert_button = QPushButton('Markdown変換')
self.mdconvert_button.clicked.connect(self.excel_to_markdown)
validate_layout_1.addWidget(self.label1)
validate_layout_1.addWidget(self.path_input_1)
validate_layout_1.addWidget(self.browse_button1)
validate_layout_1.addWidget(self.mdconvert_button)
layout.addLayout(validate_layout_1)
# SecretariatAccount CSVファイル
# validate_layout_2 = QHBoxLayout()
# self.label2 = QLabel('SA.csv')
# self.label2.setFixedWidth(100)
# self.path_input_2 = QLineEdit()
# self.browse_button2 = QPushButton('参照')
# self.browse_button2.clicked.connect(self.browse_file_2)
# validate_layout_2.addWidget(self.label2)
# validate_layout_2.addWidget(self.path_input_2)
# validate_layout_2.addWidget(self.browse_button2)
# layout.addLayout(validate_layout_2)
# Subsidy CSVファイル
validate_layout_3 = QHBoxLayout()
self.label3 = QLabel('変換Markdownファイル')
self.label3.setFixedWidth(100)
self.path_input_3 = QLineEdit()
self.browse_button3 = QPushButton('参照')
self.browse_button3.clicked.connect(self.browse_file_3)
self.excelconvert_button = QPushButton('EXCEL変換')
self.excelconvert_button.clicked.connect(self.markdown_to_excel)
validate_layout_3.addWidget(self.label3)
validate_layout_3.addWidget(self.path_input_3)
validate_layout_3.addWidget(self.browse_button3)
validate_layout_3.addWidget(self.excelconvert_button)
layout.addLayout(validate_layout_3)
# Workflow CSVファイル
validate_layout_4 = QHBoxLayout()
self.label4 = QLabel('Workflow.csv')
self.label4.setFixedWidth(100)
self.path_input_4 = QLineEdit()
self.browse_button4 = QPushButton('参照')
self.browse_button4.clicked.connect(self.browse_file_4)
validate_layout_4.addWidget(self.label4)
validate_layout_4.addWidget(self.path_input_4)
validate_layout_4.addWidget(self.browse_button4)
layout.addLayout(validate_layout_4)
# 実行ボタン
# convert_button = QPushButton('実行')
# convert_button.clicked.connect(self.excel_to_markdown)
# layout.addWidget(convert_button)
self.setLayout(layout)
def browse_file_1(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'EXCELファイルを選択', '', 'EXCEL Files (*.xlsx)')
if file_path:
self.path_input_1.setText(file_path)
def browse_file_2(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'CSVファイルを選択', '', 'CSV Files (*.csv)')
if file_path:
self.path_input_2.setText(file_path)
def browse_file_3(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'Markdownファイルを選択', '', 'Markdown Files (*.md)')
if file_path:
self.path_input_3.setText(file_path)
def browse_file_4(self):
file_path, _ = QFileDialog.getOpenFileName(self, 'CSVファイルを選択', '', 'CSV Files (*.csv)')
if file_path:
self.path_input_4.setText(file_path)
def excel_to_markdown(self):
try:
excel_file = self.path_input_1.text().strip()
if not excel_file:
QMessageBox.warning(self, '警告', '変換Excelファイル名を指定してください。')
return
sheet_name = "Sheet1"
output_file = os.path.splitext(excel_file)[0] + ".md"
"""Excel を Markdown に変換"""
df = pd.read_excel(excel_file, sheet_name=sheet_name)
# NaN を空文字に置換
df = df.fillna("")
markdown = df.to_markdown(index=False)
if output_file:
with open(output_file, "w", encoding="utf-8") as f:
f.write(markdown)
QMessageBox.information(self, '終了', f"Excel → Markdown 変換完了: {output_file}")
else:
print(markdown)
except Exception as e:
QMessageBox.critical(self, 'エラー', f"変換に失敗しました。:\n{str(e)}")
def markdown_to_excel(self):
try:
markdown_file = self.path_input_3.text().strip()
if not markdown_file:
QMessageBox.warning(self, '警告', '変換Markdownファイル名を指定してください。')
return
sheet_name = "Sheet1"
output_file = os.path.splitext(markdown_file)[0] + "_md2excel.xlsx"
"""Markdown を Excel に変換"""
with open(markdown_file, "r", encoding="utf-8") as f:
markdown_text = f.read()
# Markdown を DataFrame に変換
df = pd.read_csv(StringIO(markdown_text), sep="|", engine="python").dropna(axis=1, how="all")
# 前後の空白を削除
df.columns = df.columns.str.strip()
df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
# Excel に保存
df.to_excel(output_file, index=False, sheet_name=sheet_name)
print(f"✅ Markdown → Excel 変換完了: {output_file}")
except Exception as e:
QMessageBox.critical(self, 'エラー', f"Markdownに失敗しました。:\n{str(e)}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ExcelMarkDownConvert()
window.show()
sys.exit(app.exec_())
# parser = argparse.ArgumentParser(description="Excel ⇄ Markdown 変換ツール")
# parser.add_argument("--excel2md", nargs=2, metavar=("EXCEL_FILE", "MD_FILE"),
# help="Excel → Markdown 変換")
# parser.add_argument("--md2excel", nargs=2, metavar=("MD_FILE", "EXCEL_FILE"),
# help="Markdown → Excel 変換")
# args = parser.parse_args()
# if args.excel2md:
# excel_to_markdown(args.excel2md[0], output_file=args.excel2md[1])
# elif args.md2excel:
# markdown_to_excel(args.md2excel[0], output_file=args.md2excel[1])
# else:
# print("使い方:")
# print(" Excel → Markdown: python script.py --excel2md input.xlsx output.md")
# print(" Markdown → Excel: python script.py --md2excel input.md output.xlsx")