質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
openpyxl

openpyxlは、Excel2007以降のファイル(xlsx/xlsm/xltx/xltm)を読み書きするためのPythonライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

903閲覧

Python 2回目以降のヘッダーが表示されてしまう。

saito5940

総合スコア63

openpyxl

openpyxlは、Excel2007以降のファイル(xlsx/xlsm/xltx/xltm)を読み書きするためのPythonライブラリです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/02/04 15:31

編集2021/02/04 15:40

環境
Windows10 Pro
Pyhoon 3.6
openpyxl 3.0.5

2つのエクセルファイルがあります。
一つは数学と英語の成績のデータ
2つ目のファイルも同じです。
この2つのエクセルファイルを一つのエクセルファイルにまとめます。
項目は同じ英語と数学です。

ヘッダーは1回目は英語と数学の項目を読み込ませて2回目以降は各セルのデータだけを処理します。
以下のコードを書きました。

import openpyxl print(openpyxl.__version__) from glob import glob wb = openpyxl.Workbook() wb.active ws = wb['Sheet'] # 変数wsの中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i+1, format_l) header= None #ヘッダーには何も入っていないと宣言しておく for file_name in glob('01_raw/*'): print(file_name) read_wb = openpyxl.load_workbook(file_name) read_ws = read_wb.worksheets[0] if not header: header = read_ws[1] header = [s.value for s in header] ws.append(header) for row in read_ws.iter_rows(min_row=2): format_l = [] for cell in row: format_l.append(cell.value) ws.append(format_l) # 中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i+1, format_l)

エラーもなく動作するのですが、
2回目、3回目のループでヘッダーを読み込んでしまいます。
ifですでのヘッダーにはデータがあるので、飛ばしてデータを処理してほしいのですが・・。
どこにミスがあるのか分かりません。

# 中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i, format_l)

を実行すると

0 [None, 'math', 'english'] 1 ['Student1', 79, 42] 2 ['Student2', 83, 39] 3 ['Student3', 35, 72] 4 ['Student4', 63, 90] 5 ['Student5', 95, 42] 6 ['Student6', 92, 75] 7 ['Student7', 81, 85] 8 ['Student8', 68, 70] 9 ['Student9', 91, 56] 10 ['Student10', 75, 100] 11 ['Student11', 57, 91] 12 ['Student12', 94, 86] 13 ['Student13', 47, 96] 14 ['Student14', 66, 63] 15 ['Student15', 47, 37] 16 ['Student16', 42, 100] 17 ['Student17', 62, 31] 18 ['Student18', 98, 41] 19 ['Student19', 48, 81] 20 ['Student20', 69, 30] 21 ['Student1', 47, 33] 22 ['Student2', 38, 32] 23 ['Student3', 62, 33] 24 ['Student4', 45, 99] 25 ['Student5', 93, 31] 26 ['Student6', 87, 78] 27 ['Student7', 90, 57] 28 ['Student8', 78, 84] 29 ['Student9', 56, 33] 30 ['Student10', 42, 97] 31 ['Student11', 92, 58] 32 ['Student12', 33, 86] 33 ['Student13', 79, 93] 34 ['Student14', 85, 100] 35 ['Student15', 30, 59] 36 ['Student16', 87, 74] 37 ['Student17', 64, 59] 38 ['Student18', 59, 58] 39 ['Student19', 43, 88] 40 ['Student20', 70, 67] 41 [None, 'math', 'english'] 42 ['Student1', 79, 42] 43 ['Student2', 83, 39] 44 ['Student3', 35, 72] 45 ['Student4', 63, 90] 46 ['Student5', 95, 42] 47 ['Student6', 92, 75] 48 ['Student7', 81, 85] 49 ['Student8', 68, 70] 50 ['Student9', 91, 56] 51 ['Student10', 75, 100] 52 ['Student11', 57, 91] 53 ['Student12', 94, 86] 54 ['Student13', 47, 96] 55 ['Student14', 66, 63] 56 ['Student15', 47, 37] 57 ['Student16', 42, 100] 58 ['Student17', 62, 31] 59 ['Student18', 98, 41] 60 ['Student19', 48, 81] 61 ['Student20', 69, 30] 62 ['Student1', 47, 33] 63 ['Student2', 38, 32] 64 ['Student3', 62, 33] 65 ['Student4', 45, 99] 66 ['Student5', 93, 31] 67 ['Student6', 87, 78] 68 ['Student7', 90, 57] 69 ['Student8', 78, 84] 70 ['Student9', 56, 33] 71 ['Student10', 42, 97] 72 ['Student11', 92, 58] 73 ['Student12', 33, 86] 74 ['Student13', 79, 93] 75 ['Student14', 85, 100] 76 ['Student15', 30, 59] 77 ['Student16', 87, 74] 78 ['Student17', 64, 59] 79 ['Student18', 59, 58] 80 ['Student19', 43, 88] 81 ['Student20', 70, 67] 82 [None, 'math', 'english'] 83 ['Student1', 79, 42] 84 ['Student2', 83, 39] 85 ['Student3', 35, 72] 86 ['Student4', 63, 90] 87 ['Student5', 95, 42] 88 ['Student6', 92, 75] 89 ['Student7', 81, 85] 90 ['Student8', 68, 70] 91 ['Student9', 91, 56] 92 ['Student10', 75, 100] 93 ['Student11', 57, 91]

41と82でヘッダーを読んで書き込んでしまいます。if文を無視します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Daregada

2021/02/04 16:10

こちらの手元で実験すると、提示されたコードのままで2つめ以降のファイルのヘッダー(1行目)は読み飛ばせています。 読み込ませるファイルの中に、「2行目以降にmathやenglishと書いてある」ものが含まれていないかよく確認してください。
saito5940

2021/02/04 18:04

うまく動作していました。問題なのは、この作業を4回もしてしまう。 データは2つのファイルを合わせて40行。 一つのファイルでまめれば40行で終わらなければならないのです。 本当は40行で終了しなければならないのに同じ作業を繰り返しして、合計280行まで書き込んでしまいます。for文の書き方がおかしいいのでしょうか。初学者なのもで。。。
guest

回答1

0

自己解決

openpyxlのバグのようでした。バージョンを下げたら解決しました。

投稿2021/02/05 15:14

saito5940

総合スコア63

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問