0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GRCのCSVを一つのExcelファイルにまとめる方法

Posted at

GRCを使用してホームページの計測をしているブロガーやマーケターの皆さん。

GRCはプランによって使用できる機能に制限がかかってしまいます。

そこで今回はGRCから保存した順位を、一つのファイルにまとめるプログラムを作りました。

※CSVファイルを利用しますが、手動でGRCから全て保存する必要があります。

GRCから保存したCSVファイルを一つのファイルにまとめる

grc.py
import pathlib
import os
import glob
import csv
import openpyxl

# パスの設定
grobalPath = pathlib.Path(os.getcwd())
drivePath = r'OneDrive\レポート'
grc = r'GRC'
year = r'2025'
Month = r'10月分'
outPutPath = os.path.join(grobalPath, drivePath, grc, year)
fullPath = os.path.join(grobalPath, drivePath, grc, year, Month)
files = glob.glob(fullPath + "/*")

# 行の設定
column = 3
count = 0

# エクセルの操作
wb = openpyxl.Workbook()
ws = wb.active

for file in files:
    # ファイル名を取得
    fileName = os.path.basename(file)
    print(fileName + 'からデータを抽出中...Sheetシートに書き込み中...')
    column += 1
    saveFile = os.path.join(outPutPath, Month + '.xlsx')
    row = 1
    count += 1

    # CSVファイルからエクセルに書き込む
    with open(file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        for site, url, word, yahoo, google in reader:
            row += 1
            if count == 1:
                ws.cell(row=row, column=1).value = site
                ws.cell(row=row, column=2).value = url
                ws.cell(row=row, column=3).value = word
                ws.cell(row=1, column=column).value = fileName.replace('.csv', '')
                ws.cell(row=row, column=column).value = "'" + google.replace('-', '100')
            else:
                ws.cell(row=1, column=column).value = fileName.replace('.csv', '')
                ws.cell(row=row, column=column).value = "'" + google.replace('-', '100')

        wb.save(saveFile)

for file in files:
    # ファイル名を取得
    fileName = os.path.basename(file)
    print(fileName + 'からデータを抽出中...' + fileName + 'シートに書き込み中...')
    wb.create_sheet(fileName)
    ws = wb[fileName]
    count = 0
    saveFile = os.path.join(outPutPath, Month + '.xlsx')
    wb.save(saveFile)

    # CSVファイルからエクセルに書き込む
    with open(file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        for site, url, word, yahoo, google in reader:
            count += 1
            ws[f"A{count}"] = site
            ws[f"B{count}"] = url
            ws[f"C{count}"] = word
            ws[f"D{count}"] = "'" + google.replace('-', '100')
            # ws[f"E{count}"] = yahoo.replace('-', '100')
            # ws[f"F{count}"] = bing.replace('-', '100')

wb.save(saveFile)


実行結果

データは隠していますが、しっかり出力されています。
grc.png
抽出したcsvファイルごとにシートを分け、ファイルの名前をシートの名前に
grc2.png
GRCの出力設定はカスタマイズできます。
下記画像がCSVファイルです。
grc3.png

モジュールのインポート

pathlib: ファイルやディレクトリのパス操作を簡単に行うためのモジュール。
os: オペレーティングシステムの機能を使用してパスの結合やディレクトリの操作を行う。
glob: ワイルドカードを使ったファイル検索を行う。
csv: CSVファイルを読み込むためのモジュール。
openpyxl: Excelファイルの作成や編集を行うためのライブラリ。

パスの設定

現在の作業ディレクトリを基に、特定のパス(OneDrive\レポート\GRC\2024\10月分)を構築します。
filesには、指定したディレクトリ内のすべてのファイル(ワイルドカード*)がリストとして格納されます。

Excelの初期設定

新しいExcelファイル(ワークブック)を作成し、最初のシート(デフォルトの「Sheet」)をアクティブにします。

最初のループ: CSVデータを「Sheet」シートに書き込む

os.path.basename(file): ファイル名のみを取得。
メッセージ出力: 現在処理しているファイル名をコンソールに出力します。
CSVファイルを開く: UTF-8エンコーディングでCSVファイルを読み込みます。
データの書き込み:
CSVの各行の値を、Excelのセルに書き込みます。
1列目~3列目: site, url, wordをそのまま記録。
google列の処理: -が含まれていれば100に置換。

2回目のループ: 各ファイルに個別のシートを作成

各CSVファイルに対応する新しいシートを作成します(シート名はファイル名と同じ)。
再度CSVファイルを読み込み、その内容を対応するシートに書き込みます。

Excelファイルの保存

Excelファイルを指定したパスに保存します。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?