6
6

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 5 years have passed since last update.

PythonでExcel新規シート作成し、複数のデータを一気に代入(openpyxl、 append)

Posted at

#概要:
PythonでExcel新規シート作成し、複数のデータを一気に代入する場合、
openpyxl ライブラリでは append が利用可能

#使い方:
例:新規シートを作成し、リストや辞書のデータを代入


import openpyxl

wb = openpyxl.Workbook()  #新規ワークブックを作成
sheet = wb.active
sheet.append(['hoge0', 'hoge1', 'hoge2'])
sheet.append([999,888,777] )
sheet.append({'A' : 'A列に書き込みたいこと', 'C' : 'C列に書き込みたいこと'})
wb.save('output_append.xlsx')

append.JPG

公式ドキュメント(append):


    def append(self, iterable):
        """Appends a group of values at the bottom of the current sheet.

        * If it's a list: all values are added in order, starting from the first column
        * If it's a dict: values are assigned to the columns indicated by the keys (numbers or letters)

        :param iterable: list, range or generator, or dict containing values to append
        :type iterable: list|tuple|range|generator or dict

        Usage:

        * append(['This is A1', 'This is B1', 'This is C1'])
        * **or** append({'A' : 'This is A1', 'C' : 'This is C1'})
        * **or** append({1 : 'This is A1', 3 : 'This is C1'})

        :raise: TypeError when iterable is neither a list/tuple nor a dict

        """

##参考情報:
「Write-only-mode」では、appendのみが利用可能で、cell()等は使えない。
write-only-mode

In a write-only workbook, rows can only be added with append(). It is not possible to write (or read) cells at arbitrary locations with cell() or iter_rows().

#感想:
ソースコードを見たところ、内部的にはfor文を回して、
逐一セルにアクセスしているだけ。ExcelのRangeに対応する機能は現状(2.5.15)弱そう。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?