ちょっと使う予定ができたpythonスクリプトのひな形をChathGPTに聞いてみてメモ
ChatGPTが便利すぎる(ノД`)・゜・。
普通に調べて同じもの作ろうとしたら5倍の時間はかかかると思います。
パワポの作成編集
pip install python-pptx
ptest.py
from pptx import Presentation
from pptx.util import Inches
# 新しいプレゼンテーションを作成
prs = Presentation()
# スライドを追加
slide_layout = prs.slide_layouts[0] # タイトルスライド
slide = prs.slides.add_slide(slide_layout)
# タイトルとサブタイトルを設定
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, PowerPoint!"
subtitle.text = "Created with python-pptx"
# 画像を追加
slide = prs.slides.add_slide(prs.slide_layouts[1]) # コンテンツスライド
img_path = 'path_to_image.png' # 画像ファイルのパス
slide.shapes.add_picture(img_path, Inches(1), Inches(1), width=Inches(5))
# プレゼンテーションを保存
prs.save('presentation.pptx')
グラフのプロット、画像保存
gtest.py
import matplotlib.pyplot as plt
import numpy as np
# データの生成
x = np.linspace(0, 10, 100)
y = np.sin(x)
z = np.cos(x)
# グラフの作成
plt.figure(figsize=(12, 6))
# 散布図を作成
scatter = plt.scatter(x, y, c=z, cmap='viridis')
plt.title('Sine and Cosine with Colorbar')
plt.xlabel('x')
plt.ylabel('sin(x)')
# カラーバーの追加
cbar = plt.colorbar(scatter)
cbar.set_label('cos(x)')
# 描画領域の調整
plt.subplots_adjust(right=0.85) # 右側のスペースを増やす
# 画像の保存
plt.savefig('sine_cosine_with_colorbar.png', dpi=300, bbox_inches='tight')
# グラフを表示する場合(必要に応じて)
# plt.show()
エクセルの作成編集1
etest.py
# python
from openpyxl import Workbook, load_workbook
# 新しいワークブックの作成
wb = Workbook()
ws = wb.active
ws['A1'] = 'Hello, Excel!'
wb.save('sample.xlsx')
# 既存のワークブックを読み込む
wb = load_workbook('sample.xlsx')
ws = wb.active
print(ws['A1'].value) # 出力: Hello, Excel!
エクセルの作成編集2
etest2.py
# python
from openpyxl import Workbook, load_workbook
# 新しいワークブックの作成
import pandas as pd
# Excelファイルを読み込む
df = pd.read_excel('sample.xlsx')
print(df)
# データを操作して新しいExcelファイルを保存
df['New Column'] = df['Existing Column'] * 2
df.to_excel('modified_sample.xlsx', index=False)
エクセルの作成編集3
etest3.py
import xlsxwriter
# 新しいExcelファイルを作成
workbook = xlsxwriter.Workbook('example.xlsx')
worksheet = workbook.add_worksheet()
# データを書き込む
worksheet.write('A1', 'Hello')
worksheet.write('A2', 'World')
workbook.close()