0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python] [openpyxl] 1つのExcelファイルから全てのシートを単独のExcelファイルに分割

Last updated at Posted at 2021-11-04

openpyxl にて、シートをコピーすることが不可能なので、いろいろ検索したが洗練されたものが見当たらなかったので、自作した。
python 初挑戦 & Qiita 初投稿なので改善点等、 あるかもしれません。 ありました。(汗)

splitsheet.py
import openpyxl
import copy

book = openpyxl.load_workbook('test.xlsx')

for sheet in book:
    file_name = sheet.title + ".xlsx"
    print(file_name)
    new_book = copy.deepcopy(book)

    for new_sheet in new_book:
        if new_sheet.title == sheet.title:
            continue
        else:
            new_book.remove(new_sheet)
        
    new_book.save(file_name)

コマンドライン引数を取るように一般化したものは、次の記事にて。

####参考

https://qiita.com/github-nakasho/items/fb9df8e423bb8784cbbd
https://kirinote.com/python-specificsheet-newbook/

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?