Skip to Content
エクセルのためのPython (Japanese Edition), 2nd Edition
book

エクセルのためのPython (Japanese Edition), 2nd Edition

by Felix Zumstein
May 2026
Intermediate
418 pages
6h 42m
Japanese
O'Reilly Media, Inc.
Content preview from エクセルのためのPython (Japanese Edition), 2nd Edition

第13章 xlwings Lite を使ったカスタム 関数

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

カスタム関数とは、SUMやAVERAGE といった組み込み関数と同じように、Excel のセル内で使用する Python 関数のことだ。聞き覚えがあると思うかもしれないが、それは第11章ですでにカスタム関数を紹介しているからだ。違いは、それらは Windows でのみ互換性があったのに対し、xlwings Lite を使ったカスタム関数は Windows、macOS、そして Web 版の Excel でも動作する点だ。

この章では、まずシンプルな「Hello World」関数から始め、その後、データ変換のための型ヒントや、カスタム関数を整理するための名前空間の使い方など、いくつかの現代的な工夫を紹介する。その後、3つのプロジェクトに取り組んでいく。

  • AI関数の作成。環境変数を使って機密性の高い資格情報を扱う方法を学ぶ

  • Hugging Faceの機械学習モデルの使用:xlwings Lite(前章参照)を駆動する制限付きPython環境「Pyodide」でどこまでできるかを実演する

  • Open-Meteoの気象データを使った作業。ここでは日付やプロットの扱い方を学ぶ

第11章で学んだことのほとんどはxlwings Liteのカスタム関数にも当てはまるが、xlwings Liteでは、プロットをセル内の画像として返すといった、Excelの最新機能を活用している。しかし、先走らずに、まずは基本から始めよう!

最新のカスタム関数

このセクションでは、デコレータの代わりに型ヒントを使用して、Excelの値をDataFrameやその他のPythonデータ型に変換する方法を説明する。また、エラーの処理方法や、関数を名前空間に整理する方法についても見ていく。だが、まずは基本から始めよう。おなじみのHello World関数から始めよう。

Hello World

「Hello World」関数を作成するには、新しいワークブックを作成し、Excelリボンの[ホーム]タブにあるボタンをクリックしてxlwings Liteを起動する。前章と同様に、エディタ内のデフォルトのサンプルコードをCtrl+A(Windows)またはCommand-A(macOS)で選択し、削除する。次に、以下のコードを記述する:

from xlwings import arg, func

@func
def hello(name):
    return f"Hello {name}!"

main.pyファイルのタブに緑色のチェックマークが表示されるまで待つ。これはファイルが保存されたことを示す。次に、関数をテストするためにセル A1 を選択し、次の数式を入力する =HELLO("xlwings")と入力し、Enterキーを押す。期待通り、数式は「Hello xlwings!」と評価される。

カスタム関数のトラブルシューティング

#NAME?呼び出そうとした際にHELLOエラーが発生する場合は、関数が正しく登録されていない。これは時折発生するが、xlwings Liteのトラブルシューティングドキュメントで説明されているように、Officeのキャッシュをクリアすることで通常は解決できる。それでも解決しない場合、またはアドインストアから他のアドインをインストールしている場合は、まずすべてのアドインをアンインストールしてから、再度インストールし直してほしい。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

大規模言語モデル:難しい部分 (Japanese Edition)

大規模言語モデル:難しい部分 (Japanese Edition)

Thársis T. P. Souza, Jonathan K. Regenstein

Publisher Resources

ISBN: 0642572395971