Convert multiple CSVs to each Excel sheet

Overview

It is difficult to browse a huge amount of CSV files. I put it together in one Excel file with Python. I often see combining CSV to the same sheet, but I can't find an example of converting 1csv = 1 sheet. I made an article as a memorandum.

environment

ubuntu(wsl) python3.6.8

code

csvtxlsx.py


import os
import glob
from pathlib import Path

import openpyxl
import csv


csvfiles = glob.glob("(Read source file path)/*.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("(Output file path)/(Output file name)")

Conclusion

Browsing and handing over files has become easier.

Recommended Posts

Convert multiple CSVs to each Excel sheet
Convert json to excel
Function to convert Excel column to number
Save pandas.DataFrame to Excel by sheet
Convert Excel data to JSON with python
Convert "number" of excel date to python datetime
Convert to HSV
Convert UTF-8 CSV files to read in Excel
Convert images in multiple folders to different pdfs for each folder at once
Convert multiple jpg files to one PDF file
[Python] Convert general-purpose container and class to each other
How to convert Json file to CSV format or EXCEL format
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row
Convert 202003 to 2020-03 with pandas
Convert kanji to kana
Convert jupyter to py
Convert keras-yolo3 to onnx
Convert dict to array
Convert Excel file to text in Python for diff purposes