山内セミナーⅠ(2021/05/05)

関連サイトと資料

サンプルプログラム(3)

test4.py
import openpyxl
  
wb = openpyxl.Workbook()
wb.save('test1.xlsx')
    

test5.py
import openpyxl
  
x = 100
y = 3.14
z = x + y
s1 = 'これは文字列です。'
   
wb = openpyxl.Workbook()
ws = wb.active
ws.title = '20210428'
   
ws.cell(row=1, column=2).value = z
ws.cell(row=2, column=1).value = s1
   
wb.save('test2.xlsx')
    

test6.py
import openpyxl
   
def odd_or_even(sheet):
    for i in range(10):
        sheet.cell(row=i+1, column=1).value = i
        if i % 2 != 0:
            sheet.cell(row=i+1, column=2).value = '奇数'
        else:
            sheet.cell(row=i+1, column=2).value = '偶数'
    
wb = openpyxl.Workbook()
ws = wb.active
ws.title = '偶数・奇数の判別'
   
odd_or_even(ws)
   
wb.save('test3.xlsx')
    

test7.py
import openpyxl
import math
  
def calcArea(r):
    area = r * r * math.pi
    return area
  
def insert(sheet, y, r):
    sheet.cell(row=y, column=1).value = r
    sheet.cell(row=y, column=2).value = calcArea(r)
  
def createTable(sheet):
    sheet.cell(row=1, column=1).value = '半径'
    sheet.cell(row=1, column=2).value = '円の面積'
    y = 2
    for r in range(10, 100, 10):
        insert(sheet, y, r)
        y = y + 1
  
wb = openpyxl.Workbook()
ws = wb.active
ws.title = '円の面積'
  
createTable(ws)
  
wb.save('test4.xlsx')
    

test8.py
import openpyxl
wb = openpyxl.load_workbook("test4.xlsx")
  
ws = wb.active
v = float(ws.cell(row=10, column=2).value)
  
print(v)
    

test9.py
import openpyxl
wb = openpyxl.load_workbook("山手線駅名.xlsx")
  
ws = wb['山手線駅名']
for y in range(1, 30+1):
    name = ws.cell(row=y, column=1).value
    print(name)
    wb.create_sheet(name)
  
wb.save("山手線駅名.xlsx")