<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>書き込み - プログラミングで遊ブログ</title>
	<atom:link href="https://lemon818.com/tag/%E6%9B%B8%E3%81%8D%E8%BE%BC%E3%81%BF/feed/" rel="self" type="application/rss+xml" />
	<link>https://lemon818.com</link>
	<description>現役システムエンジニアが趣味でプログラミングする自由気ままなブログ</description>
	<lastBuildDate>Sat, 01 Aug 2020 14:33:23 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>	<item>
		<title>【Python】Excelファイルを新規作成・読み込み・書き込み処理まとめ</title>
		<link>https://lemon818.com/pythop-excel/</link>
		
		<dc:creator><![CDATA[Take]]></dc:creator>
		<pubDate>Fri, 31 Jul 2020 03:58:21 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[新規作成]]></category>
		<category><![CDATA[書き込み]]></category>
		<category><![CDATA[読み込み]]></category>
		<guid isPermaLink="false">https://lemon818.com/?p=5196</guid>

					<description><![CDATA[Python から Excel ファイルを作成してみたい！   &#160; そんな思いから Python から Excel ファイルを操作（新規作成・読み込み・書き込み）する処理を簡単にまとめました。 どーもTakeで…]]></description>
										<content:encoded><![CDATA[<div class="sc_frame_wrap solid yellow">
<div class="sc_frame "><strong><span style="color: #000000;">Python から Excel ファイルを作成してみたい！  </span></strong></div>
</div>
<p>&nbsp;</p>
<p>そんな思いから Python から Excel ファイルを操作（<strong>新規作成・読み込み・書き込み</strong>）する処理を簡単にまとめました。</p>
<p>どーもTakeです。</p>
<p>&nbsp;</p>
<p>Python から Excel ファイル ができれば、Python で Webスクライピングをした結果を Excel ファイル 出力させたりとか、</p>
<p>いろんなことに応用できると思います。</p>
<p>&nbsp;</p>
<p>この記事では、Excel ファイルを操作（新規作成・読み込み・書き込み）を具体的な<span style="color: #0000ff;"><strong>ソースコードをもとに</strong></span>説明します。</p>
<p>&nbsp;</p>
<div class="sc_frame_wrap block blue">
<div class="sc_frame_title">この記事でわかること！</div>
<div class="sc_frame ">
<div class="sc_frame_text">
<div class="sc_designlist ol square solid blue">
<ol>
<li>準備 ・・・ Python で Excel ファイルを処理するための準備</li>
<li>Excel ファイルの新規作成</li>
<li>Excel ファイルの書き込み（新規ファイル、既存ファイルへの出力）</li>
<li>Excel ファイルの読み込み</li>
</ol>
</div>
</div>
</div>
</div>
<p>&nbsp;</p>
<h2>準備</h2>
<p>Python で Excel ファイルを処理するために「<span>openpyxl</span>」をインポートします。</p><pre class="crayon-plain-tag">pip install openpyxl</pre><p>
&nbsp;</p>
<p>pip コマンドでインポートできます。これで準備OKです。</p>
<p>あとはソースコード冒頭で 「<span>openpyxl</span>」モジュールを取り込むようにします。</p><pre class="crayon-plain-tag">import openpyxl as excel</pre><p>
&nbsp;</p>
<h2>Excel ファイルの新規作成</h2>
<p>Python で Excel ファイルを新規作成する方法です。</p>
<p>&nbsp;</p>
<p>下記ソースコードのように、「openpyxl」モジュールの Workbook メソッドで簡単に新規作成できます。</p>
<p>あとは save メソッドで引数に Excel ファイルのファイル名を指定すれば OK です。（下記では test.xlsx と指定）</p><pre class="crayon-plain-tag">import openpyxl as excel

wbname = "test.xlsx"
wb = excel.Workbook()
wb.save("test.xlsx")</pre><p>
&nbsp;</p>
<p>下記のように「test.xlsx」が出力されていることが確認できます。</p>
<p><img fetchpriority="high" decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel.png" alt="" width="620" height="172" class="aligncenter size-full wp-image-5236" srcset="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel.png 620w, https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel-300x83.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></p>
<p>&nbsp;</p>
<h2>Excel ファイルの書き込み</h2>
<p>Python で Excel ファイルを新規作成し、書き込む方法です。</p>
<p>&nbsp;</p>
<p>出力先の Excel ファイルのシートのセル番号（ws["A1"] 、ws["B2"] ）を指定し、そこに値を代入すれば OK です。</p><pre class="crayon-plain-tag">import openpyxl as excel

wbname = "test.xlsx"
msg_A1 = "Helllo World"
msg_B2 = "あいうえお"

wb = excel.Workbook()
ws = wb.active

# 書き込み処理
ws["A1"] = msg_A1
ws["B2"] = msg_B2

wb.save(wbname)</pre><p>
&nbsp;</p>
<p>上記を実行することで「test.xlsx」が新規作成され、そのファイルに指定した内容が書き込みされていることが確認できます。</p>
<p>&nbsp;</p>
<p><img decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel2.png" alt="" width="445" height="287" class="aligncenter size-full wp-image-5244" srcset="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel2.png 445w, https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel2-300x193.png 300w" sizes="(max-width: 445px) 100vw, 445px" /></p>
<p>&nbsp;</p>
<h3>既存 Excel ファイルへの書き込み</h3>
<p>既存 Excel ファイルへの書き込み方についてです。</p>
<p>&nbsp;</p>
<p>新規作成の処理とは少し異なり、openpyxl モジュールの load_workbook メソッドを使い既存ファイルを開いてから書き込み処理をします。</p>
<p>下記ソースコードでは、既存ファイル「test.xlsx」の C5 セルに「test」と書き込むように処理します。</p><pre class="crayon-plain-tag">import openpyxl as excel

wbname = "test.xlsx"
msg = "test"

wb = excel.load_workbook(wbname)
ws = wb.active

# 書き込み処理
ws["C5"] = msg

wb.save(wbname)</pre><p>
&nbsp;</p>
<p>下記のように「C5」セルに「test」と書き込まれることが確認できます。</p>
<p><img decoding="async" src="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel4.png" alt="" width="849" height="293" class="aligncenter wp-image-5249" srcset="https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel4.png 901w, https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel4-300x104.png 300w, https://lemon818.com/wp/wp-content/uploads/2020/07/python-excel4-768x265.png 768w" sizes="(max-width: 849px) 100vw, 849px" /></p>
<h2>Excel ファイルの読み込み</h2>
<p>Python で既存 Excel ファイルを読み込む方法です。</p>
<p>&nbsp;</p>
<p>openpyxl モジュールの load_workbook メソッドを使えば、既存ファイルを開くことが可能です。</p>
<p>下記にソースコードを示します。</p><pre class="crayon-plain-tag">import openpyxl as excel

wbname = "test.xlsx"
wb = excel.load_workbook(wbname)

# シート単位にループ
for sheet in wb.sheetnames: 
    print("シート名:" + sheet)
    ws = wb[sheet]
    # 行単位にループ
    for row in ws.rows:
        # 列単位にループ
        for cell in row:
            print("行:" + str(cell.row) + " 列:" + str(cell.column))
            print("値:" + str(cell.value))</pre><p>
&nbsp;</p>
<p>上記は「test.xlsx」を開き、その中身を表示させる処理です。</p>
<p>「load_workbook」メソッドをつかってファイルを開きシート・行・列単位に値を表示しています。</p>
<p>&nbsp;</p>
<p>下記表に取得値についてまとめてます（「cell.XXX」の「cell」は<strong>独自に定義したもの</strong>なので、<strong>作成するソースに応じて変更してください</strong>）。</p>
<p>よければ参考にしてみてください（詳細は<a href="https://openpyxl.readthedocs.io/en/stable/index.html">このページ</a>を参考にいただければと思います）。</p>
<p>&nbsp;</p>
<table style="border-collapse: collapse; width: 64.6084%; height: 238px;">
<tbody>
<tr style="height: 48px;">
<td style="width: 22.5368%; text-align: center; height: 48px; background-color: #21226e;"><span style="color: #ffffff;"><strong>値</strong></span></td>
<td style="width: 20.6785%; text-align: center; height: 48px; background-color: #21226e;"><span style="color: #ffffff;"><strong>意味</strong></span></td>
<td style="width: 24.6793%; text-align: center; height: 48px; background-color: #21226e;"><span style="color: #ffffff;"><strong>例</strong></span></td>
</tr>
<tr style="height: 46px;">
<td style="width: 22.5368%; height: 46px; background-color: #ffffe6;"><strong>cell.value</strong></td>
<td style="width: 20.6785%; height: 46px;"><strong>セルの値</strong></td>
<td style="width: 24.6793%; height: 46px;"><strong>あいうえお</strong></td>
</tr>
<tr style="height: 48px;">
<td style="width: 22.5368%; height: 48px; background-color: #ffffe6;"><strong>cell.coordinate</strong></td>
<td style="width: 20.6785%; height: 48px;"><strong>セル番号</strong></td>
<td style="width: 24.6793%; height: 48px;"><strong>A2</strong></td>
</tr>
<tr style="height: 48px;">
<td style="width: 22.5368%; height: 48px; background-color: #ffffe6;"><strong>cell.column</strong></td>
<td style="width: 20.6785%; height: 48px;"><strong>セルの列</strong></td>
<td style="width: 24.6793%; height: 48px;"><strong>1</strong></td>
</tr>
<tr style="height: 48px;">
<td style="width: 22.5368%; height: 48px; background-color: #ffffe6;"><strong>cell.row</strong></td>
<td style="width: 20.6785%; height: 48px;"><strong>セルの行</strong></td>
<td style="width: 24.6793%; height: 48px;"><strong>2</strong></td>
</tr>
<tr>
<td style="width: 22.5368%; background-color: #ffffe6;"><strong>cell.parent</strong></td>
<td style="width: 20.6785%;"><strong>参照元の情報</strong></td>
<td style="width: 24.6793%;"><strong>&lt;Worksheet "Sheet"&gt;</strong></td>
</tr>
<tr>
<td style="width: 22.5368%; background-color: #ffffe6;"><strong>cell.encoding</strong></td>
<td style="width: 20.6785%;"><strong>文字コード</strong></td>
<td style="width: 24.6793%;"><strong>utf-8</strong></td>
</tr>
</tbody>
</table>
<p>&nbsp;</p>
<h2>最後に</h2>
<p>いかがでしたでしょうか？</p>
<p>この記事では Python から Excel ファイルを操作（<strong>新規作成・読み込み・書き込み</strong>）について解説しました。</p>
<p>&nbsp;</p>
<p>この記事が読者の Python 開発のお役に立てれば幸いです。</p>
<p>ではでは。</p>
<p>&nbsp;</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
