LoginSignup
0
0

More than 1 year has passed since last update.

Excelより情報を取得し、任意のフォルダにファイルを作成する

Posted at

概要

Python勉強中です。
Excelよりフォルダを読み取ってファイルを作成します。

import os
import pathlib
import openpyxl

WORK_BOOK = r"C:\work\it\python\sample_image\sample.xlsx"
TARGET_SHEET = r"test2"
COLUMN_INDEX = 14

wb = openpyxl.load_workbook(WORK_BOOK, data_only=True)
ws = wb[TARGET_SHEET]

for row_no in range(1, ws.max_row + 1):
    id = ws.cell(row_no, COLUMN_INDEX).value

    if (not id) or (not id is None):
        folder_path = ('.\\' + str(id))
        output_path = pathlib.Path(folder_path + '\\output.txt')

        # フォルダがなかったら作成
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

        # ファイルが存在しなかったら作成
        if not os.path.exists(output_path):
            with open(output_path, 'w', encoding="utf-8") as w:
                w.write('test')
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