LoginSignup
6
8

More than 3 years have passed since last update.

複数のCSVを各エクセルシートに変換

Posted at

概要

膨大な量のCSVファイルを閲覧するのには難があります。
Pythonで一つのエクセルファイルにまとめました。
CSVを同じシートに結合はよくみかけますが、1csv=1シートの変換は例が見つからず、
備忘録をかねて記事にしました。

環境

ubuntu(wsl)
python3.6.8

コード

csvtxlsx.py
import os
import glob
from pathlib import Path

import openpyxl
import csv


csvfiles = glob.glob("(読み込み元ファイルパス)/*.csv", recursive=False)
wb = openpyxl.Workbook()
for file in csvfiles:
    wb.create_sheet(os.path.splitext(os.path.basename(file))[0])
    wb.active = wb.sheetnames.index(os.path.splitext(os.path.basename(file))[0])
    ws = wb.active
    with open(file, encoding="shift-jis") as f:
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            ws.append(row)
wb.save("(出力先ファイルパス)/(出力ファイル名)")

結論

閲覧やファイル受け渡しが楽になりました。

6
8
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8