Skip to content

Instantly share code, notes, and snippets.

@mehori
Last active January 2, 2025 02:00
Show Gist options
  • Select an option

  • Save mehori/11e0a9d6c705e2da4d8d8083aa46ba0e to your computer and use it in GitHub Desktop.

Select an option

Save mehori/11e0a9d6c705e2da4d8d8083aa46ba0e to your computer and use it in GitHub Desktop.
csv ファイルで与えた「パス、カテゴリ、タグ(複数)」で frontmatter を書き換えて出力する python スクリプト
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