実現したいこと
ここに実現したいことを箇条書きで書いてください。
- pythonのopenpyxlを用いて規格をそろえた散布図を、Excelワークブック内の全シートについて作成したい
前提
プログラミングを触り始めて1か月弱の初級者です。
研究の中で同じような散布図を作る機会が多くなりそうなので、pythonのopenpyxlを用いて、Excelのxyデータをもとにタイトルや軸の間隔などの規格をそろえたグラフを自動的に作成できるようプログラムを書いています。
基本的なグラフの作り方については↓を
Excelシートのループについては以下のサイトを参考にしました。
https://qiita.com/AkioTakamoto/items/9d2ff4c7cc077d59d607
グラフ単体の出力まではうまくいったのですがすべてのシートに対して同様に処理しようとするとリストが出力される以外何も起きませんでした。どこが間違っているか、はたまた根本的にやり方を変えた方が良いかなどありましたらご教示いただけますと幸いです。
どうぞよろしくお願いいたします。
※プライバシー保護のため軸ラベルなどの名前は適当にしていますことご了承ください
発生している問題・エラーメッセージ
エラーメッセージは出ていません。ただ、指定したワークブックのシートのリストが出力される以外何も起きません。
該当のソースコード
python
1# -*- coding: utf-8 -*- 2#必要なモジュールをインポートする 3import openpyxl 4from openpyxl import Workbook, load_workbook 5from openpyxl.chart import ScatterChart, LineChart, Reference, Series 6from openpyxl.chart.shapes import GraphicalProperties 7from openpyxl.chart.layout import Layout, ManualLayout 8from openpyxl.chart.text import RichText 9from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font, RichTextProperties 10from openpyxl.drawing.line import LineProperties 11from copy import deepcopy 12 13File="matome.xlsx" 14 15#使うエクセルファイルを指定する。 16wb = openpyxl.load_workbook(File) 17 18#Book内のすべてのsheet名リストを取得 19sheetnames = wb.sheetnames 20print(sheetnames) 21 22#sheet名リストから1つずつsheet名を取りだす 23for sheetname in sheetnames: 24 sheet = wb[sheetname] 25 #散布図のインスタンスの作成。グラフの大きさ、タイトル、軸ラベル、スタイル指定 26 chart = ScatterChart() 27 chart.title = "散布図" 28 chart.style = 13 29 chart.x_axis.title = 'x[m]' 30 chart.y_axis.title = 'y[m]' 31 chart.height = 15 32 chart.width = 25 33 #列の指定、行の初めの指定、行の終わりの指定(上、左からn番目) 34 xvalues = Reference(ws, min_col=2, min_row=2, max_row=28) 35 values = Reference(ws, min_col=3, min_row=2, max_row=28) 36 #グラフ化 37 series = Series(values, xvalues, title_from_data=True) 38 #作成したチャートに追加 39 chart.series.append(series) 40 #マーカーの種類・サイズ・色合いを変える。ラインの色を変える 41 chart.series[0].marker.symbol = "triangle" 42 chart.series[0].marker.size = 8 43 chart.series[0].marker.graphicalProperties.solidFill = "FF0000" 44 chart.series[0].marker.graphicalProperties.line.solidFill = "0000FF" 45 chart.series[0].graphicalProperties.line.solidFill = "0000FF" 46 47 48 49 #XYそれぞれの軸の最大・最少を指定し、グラフの範囲を規定する。また、目盛の間隔を指定する 50 chart.x_axis.scaling.min = 0 51 chart.y_axis.scaling.min = 0 52 chart.x_axis.scaling.max = 250 53 chart.y_axis.scaling.max = 250 54 chart.x_axis.majorUnit = 25 55 chart.y_axis.majorUnit = 50 56 #X軸とY軸のフォーマットを変更する それぞれの色やプロットの種類 wは謎 57 chart.x_axis.tickLblPos = "low" 58 chart.x_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700, prstDash="dot")) 59 chart.y_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700)) 60 #XYのグリッド線をオフにする 61 chart.x_axis.majorGridLines = None 62 chart.y_axis.majorGridLines = None 63 #タイトルとx軸, y軸のラベルの書式を変える 64 char_properties = CharacterProperties(latin=Font(typeface='Meiryo UI'), sz=1400, b=True, solidFill="000000") 65 paragraph_properties = ParagraphProperties(defRPr=char_properties) 66 chart.title.tx.rich.p[0].pPr = paragraph_properties 67 chart.x_axis.title.tx.rich.p[0].pPr = paragraph_properties 68 chart.y_axis.title.tx.rich.p[0].pPr = paragraph_properties 69 70 chart.x_axis.title.layout = Layout(ManualLayout(x = 0.45, y = 0.92, xMode="edge", yMode="edge")) 71 chart.y_axis.title.layout = Layout(ManualLayout(x = 0.02, y = 0.5, xMode="edge", yMode="edge")) 72 73 74 #グラフを描画する位置をセルで指定する。 75 ws.add_chart(chart, "D2") 76 77 78#ファイルを保存する 79wb.save('matome.xlsx') 80 81#1つのシートについてグラフ単体の出力に成功したソースコード 82# -*- coding: utf-8 -*- 83#いろいろ必要なモジュールをインポートする 84import openpyxl 85from openpyxl import Workbook, load_workbook 86from openpyxl.chart import ScatterChart, LineChart, Reference, Series 87from openpyxl.chart.shapes import GraphicalProperties 88from openpyxl.chart.layout import Layout, ManualLayout 89from openpyxl.chart.text import RichText 90from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font, RichTextProperties 91from openpyxl.drawing.line import LineProperties 92from copy import deepcopy 93 94#使うエクセルファイル、シートを指定する。ワークシートの名前も指定する。 95wb = load_workbook('matome.xlsx') 96ws = wb['A'] 97ws.title = 'A' 98#散布図のインスタンスの作成。グラフの大きさ、タイトル、軸ラベル、スタイル指定 99chart = ScatterChart() 100chart.title = "散布図" 101chart.style = 13 102chart.x_axis.title = 'x[m]' 103chart.y_axis.title = 'y[m]' 104chart.height = 15 105chart.width = 25 106#列の指定、行の初めの指定、行の終わりの指定(上、左からn番目) 107xvalues = Reference(ws, min_col=2, min_row=2, max_row=28) 108values = Reference(ws, min_col=3, min_row=2, max_row=28) 109#グラフ化 110series = Series(values, xvalues, title_from_data=True) 111#作成したチャートに追加 112chart.series.append(series) 113#マーカーの種類・サイズ・色合いを変える。ラインの色を変える 114chart.series[0].marker.symbol = "triangle" 115chart.series[0].marker.size = 8 116chart.series[0].marker.graphicalProperties.solidFill = "FF0000" 117chart.series[0].marker.graphicalProperties.line.solidFill = "0000FF" 118chart.series[0].graphicalProperties.line.solidFill = "0000FF" 119 120 121 122#XYそれぞれの軸の最大・最少を指定し、グラフの範囲を規定する。また、目盛の間隔を指定する 123chart.x_axis.scaling.min = 0 124chart.y_axis.scaling.min = 0 125chart.x_axis.scaling.max = 250 126chart.y_axis.scaling.max = 250 127chart.x_axis.majorUnit = 25 128chart.y_axis.majorUnit = 50 129#X軸とY軸のフォーマットを変更する それぞれの色やプロットの種類 wは謎 130chart.x_axis.tickLblPos = "low" 131chart.x_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700, prstDash="dot")) 132chart.y_axis.majorGridlines.spPr = GraphicalProperties(ln=LineProperties(solidFill= "696969", w=0.01*12700)) 133#XYのグリッド線をオフにする 134chart.x_axis.majorGridLines = None 135chart.y_axis.majorGridLines = None 136#タイトルとx軸, y軸のラベルの書式を変える 137char_properties = CharacterProperties(latin=Font(typeface='Meiryo UI'), sz=1400, b=True, solidFill="000000") 138paragraph_properties = ParagraphProperties(defRPr=char_properties) 139chart.title.tx.rich.p[0].pPr = paragraph_properties 140chart.x_axis.title.tx.rich.p[0].pPr = paragraph_properties 141chart.y_axis.title.tx.rich.p[0].pPr = paragraph_properties 142 143chart.x_axis.title.layout = Layout(ManualLayout(x = 0.45, y = 0.92, xMode="edge", yMode="edge")) 144chart.y_axis.title.layout = Layout(ManualLayout(x = 0.02, y = 0.5, xMode="edge", yMode="edge")) 145 146#凡例のフォーマットを変更する リッチテキストとして取り扱う 147char_properties = CharacterProperties(latin=Font(typeface='Meiryo UI'), sz=800, b=False) 148paragraph_properties = ParagraphProperties(defRPr=char_properties) 149rich_text = RichText(p=[Paragraph(pPr=paragraph_properties, endParaRPr=char_properties)]) 150chart.legend.txPr = rich_text 151 152#凡例の置く位置を変える 最少が0、最大が1 弄ってみるしかない 153chart.legend.layout = Layout(manualLayout = ManualLayout( 154 yMode='edge', 155 xMode='edge', 156 x=0.9, y=0.1, 157 h=0.1, w=0.1 158 )) 159 160#グラフを置く位置をセルで指定する。 161ws.add_chart(chart, "D2") 162 163#指定した場所にセーブする 164wb.save('matome.xlsx')
試したこと
最後の「ファイルを保存する」もインテンドを下げて実行した→リストの出力すらなくなった。エラーも出ない。インテンドを下げないままではExcelファイルを開いたままだと出ていたpermission deniedのエラーも出なくなった。
補足情報(FW/ツールのバージョンなど)
windows10、テキストエディタはjupyter noteを使用。 Anacondaは未インストール Python3.11.1
回答1件
あなたの回答
tips
プレビュー