teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

途中までしか反映されていなかったので、修正しました。

2021/03/04 06:05

投稿

manson1120
manson1120

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,11 +1,11 @@
1
1
  ### 実現したいこと
2
- パワーポイントを自動で
2
+ **パワーポイントを自動で繰り返し処理を用いて作成したい**
3
3
 
4
- エクセルの情報を抽出
4
+ 1.エクセルの情報を抽出
5
5
 
6
- 1つのパワーポファイルに複数のページを作成
6
+ 2.エクセルから抽出した情報をタイトルとテキスト反映
7
7
 
8
- エクセルから抽出した情報をタイトルとテキスト反映
8
+ 3.2番目デー分繰り返し、1つのパワーポファイルに複数のページを作成
9
9
 
10
10
  ### できている内容
11
11
  ・エクセルのデータを抽出してlistに格納
@@ -13,28 +13,147 @@
13
13
 
14
14
  ### できない内容
15
15
  ・エクセルから抽出した情報をタイトルとテキストにいれる
16
- ・繰り返し処理のコード
16
+ ※「パワーポイント自動作成コード」に記載の★箇所にエクセルで取得したデータを入れて、PPTシートをデータ分作成することが出来ない。
17
17
 
18
18
  ### 補足情報(FW/ツールのバージョンなど)
19
- pythonバージョン ⇒ 3.6
19
+ pythonバージョン ⇒ Python 3.8.0
20
20
  python実行環境 ⇒ windows10
21
21
  file構成 ⇒ デスクトップに「python」ディレクトリを設置
22
- pythonディレクトリの設置物 ⇒ pythonコードファイル、エクセルファイル、テンプレート用PPT
22
+ pythonディレクトリの設置物 ⇒ pythonコードファイル、エクセルファイル、テンプレート用PPT
23
23
  エクセルファイル名 ⇒ data.xlsx
24
24
  PPTファイル名 ⇒ templat.pptx
25
25
 
26
- ### パワーポイント自動作成
26
+ ### エクセル情報
27
+ ![イメージ説明](af4884446ce751cf1c44142b5e9aa8d7.png)
27
28
 
29
+ ### パワーポイント自動作成コード
30
+
28
31
  ```
32
+ # coding: cp932
33
+
34
+ # ---必要なモジュールをimport---
35
+ from pptx import Presentation
36
+ from pptx.chart.data import ChartData
37
+ from pptx.enum.chart import XL_CHART_TYPE
38
+ from pptx.util import Inches
39
+ from pptx.util import Pt, Inches
40
+
41
+ # ---大きさの単位変更---
42
+ # センチメートルをPowerPoint上の距離に変換する関数
43
+ def Centis(length):
44
+ centi = Inches(length/2.54)
45
+ return centi
46
+
47
+ # ---スライド作成---
48
+ # 1枚のスライドでプレゼンテーションを作成する※今回はPPTを指定した
49
+ prs = Presentation('templat.pptx')
50
+
51
+ # 1枚目のスライドのレイアウトを使用するように指定
52
+ title_only_slide_layout = prs.slide_layouts[1]
53
+ slide = prs.slides.add_slide(title_only_slide_layout)
54
+ shapes = slide.shapes
55
+
56
+ # タイトルに入力したい文字列
57
+ slide_title = '★C列の題名を入れたい★'
58
+ shapes.title.text = slide_title
59
+
60
+ # ----------
61
+ # 前月のテキストボックスの位置を決める
62
+ left_Sub = Centis(1.0)
63
+ top_Sub = Centis(2.3)
64
+
65
+ # 前月のテキストボックスの幅と高さを決める
66
+ width_Sub = Centis(4.4)
67
+ height_Sub = Centis(0.86)
68
+ txBoxSub = slide.shapes.add_textbox(left_Sub, top_Sub, width_Sub, height_Sub)
69
+
70
+ tfSub = txBoxSub.text_frame
71
+ tfSub.text = '★D列のサブ題名を入れたい★'
72
+
73
+ # フォントサイズ・書体名変更
74
+ tfSub.paragraphs[0].font.size = Pt(14)
75
+ tfSub.paragraphs[0].font.name = 'メイリオ'
76
+
77
+ # ----------
78
+ # 前月のテキストボックスの位置を決める
79
+ left = Centis(2.17)
80
+ top = Centis(2.97)
81
+
82
+ # 前月のテキストボックスの幅と高さを決める
83
+ width = Centis(2.17)
84
+ height = Centis(0.86)
85
+ txBox = slide.shapes.add_textbox(left, top, width, height)
86
+
87
+ tf = txBox.text_frame
88
+ tf.text = "★A列の前月を入れたい★"
89
+
90
+ # フォントサイズ・書体名変更
91
+ tf.paragraphs[0].font.size = Pt(14)
92
+ tf.paragraphs[0].font.name = 'メイリオ'
93
+
94
+ # 今月のテキストボックスの位置を決める
95
+ left_Month = Centis(2.17)
96
+ top_Month = Centis(10.79)
97
+
98
+ # 今月のテキストボックスの幅と高さを決める
99
+ width_Month = Centis(2.17)
100
+ height_Month = Centis(0.86)
101
+ txBoxMonth = slide.shapes.add_textbox(left_Month, top_Month, width_Month, height_Month)
102
+
103
+ tfMonth = txBoxMonth.text_frame
104
+ tfMonth.text = '★B列を入れたい★'
105
+
29
- エラーメッセージ
106
+ # フォントサイズ変更
107
+ tfMonth.paragraphs[0].font.size = Pt(14)
108
+ tfMonth.paragraphs[0].font.name = 'メイリオ'
109
+
110
+ prs.save('chart-01.pptx')
111
+
30
112
  ```
31
113
 
32
114
  ### エクセルデータをlistに格納
33
115
 
34
116
  ```ここに言語名を入力
117
+ import openpyxl
118
+
119
+ # ブック取得
120
+ wb = openpyxl.load_workbook("data.xlsx")
35
- スコード
121
+ # シトを取得
122
+ sheet = wb["Sheet1"]
123
+
124
+ #A列のデータのみ取り出す ※10月
125
+ #値の入っている最大行、最大列まで行単位でセルの値を取得する
126
+ for rows in sheet.iter_rows(min_row=2, min_col=1, max_col=1):
127
+ for cell in rows:
128
+ list_A = cell.value
129
+ print(list_A)
130
+
131
+ #B列のデータのみ取り出す ※11月
132
+ #値の入っている最大行、最大列まで行単位でセルの値を取得する
133
+ for rows in sheet.iter_rows(min_row=2, min_col=2, max_col=2):
134
+ for cell in rows:
135
+ list_B = cell.value
136
+ print(list_B)
137
+
138
+ #C列のデータのみ取り出す ※title
139
+ #値の入っている最大行、最大列まで行単位でセルの値を取得する
140
+ for rows in sheet.iter_rows(min_row=2, min_col=3, max_col=3):
141
+ for cell in rows:
142
+ list_C = cell.value
143
+ print(list_C)
144
+
145
+ #D列のデータのみ取り出す ※sub title
146
+ #値の入っている最大行、最大列まで行単位でセルの値を取得する
147
+ for rows in sheet.iter_rows(min_row=2, min_col=4, max_col=4):
148
+ for cell in rows:
149
+ list_D = cell.value
150
+ print(list_D)
36
151
  ```
152
+ ### 最終イメージPPT
153
+ ※下記PPTシートをエクセルシート7行目の情報分作成する
154
+ ![イメージ説明](28e920099da562a17553051aa38af5fa.png)
37
155
 
38
156
  ### 試したこと
39
157
 
158
+ forを使用して繰り返し処理を行おうと思いましたが、プログラムを作った経験が今回が初めてで、どのように書いたらいいのかイメージが湧きませんでした。
40
- ここに問題に対して試しことを記載してくい。
159
+ お力添えいただけると幸です