Last active
January 2, 2025 02:00
-
-
Save mehori/11e0a9d6c705e2da4d8d8083aa46ba0e to your computer and use it in GitHub Desktop.
csv ファイルで与えた「パス、カテゴリ、タグ(複数)」で frontmatter を書き換えて出力する python スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| import frontmatter | |
| import shutil | |
| import sys | |
| def update_frontmatter(csv_file): | |
| with open(csv_file, 'r') as file: | |
| reader = csv.reader(file) | |
| for row in reader: | |
| try: | |
| filepath, category, *tags = row | |
| filepath = filepath + 'index.md' | |
| category = category.strip() | |
| tags = [tag.strip() for tag in tags] | |
| backup_filepath = filepath + '~' | |
| shutil.copy2(filepath, backup_filepath) | |
| post = frontmatter.load(filepath) | |
| post['categories'] = category | |
| post['tags'] = tags | |
| with open(filepath, 'wb') as f: | |
| frontmatter.dump(post, f) | |
| except Exception as e: | |
| print(f"Error processing {filepath}: {e}") | |
| if __name__ == "__main__": | |
| if len(sys.argv) != 2: | |
| print("Usage: python script.py <csv_file>") | |
| sys.exit(1) | |
| csv_file = sys.argv[1] | |
| update_frontmatter(csv_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment