王薰珮python輸入openpyxl處理EXCEL檔案



from openpyxl import load_workbook   #王薰珮指導蕭穎婕等學生專題
book = load_workbook(filename='pylenin.xlsx')
sheet = book['20200101p']     #工作表
rowBegin = 4                  #起始列
rowEnd = 10                  #結束列780
criticalInc = 0.01
criticalDes = 0.01
localH = sheet.cell(row=rowBegin, column=3).value
localL = sheet.cell(row=rowBegin, column=4).value
status = 0
wealth = 0
for x, row in enumerate(sheet.iter_rows(min_row = rowBegin, max_row = rowEnd)):
    rowNo = x + rowBegin        #串列編號 x 起始於 0
    dayH = row[2].value         #當日最高C欄
    dayL = row[3].value         #當日最低D欄
    dayC = row[4].value         #當日收盤E欄
    if status == -1:            #檢測波段高
        localH = dayH
    elif localH < dayH:         
        localH = dayH
    if status == 1:             #檢測波段低
        localL = dayL
    elif localL > dayL:
        localL = dayL
    sheet.cell(row = rowNo, column = 6).value = localH #寫入波段高於F欄
    sheet.cell(row = rowNo, column = 7).value = localL #寫入波段低於G欄
    if status < 1 and dayC > (1 + criticalInc)*localL:
        status = 1
    elif status > -1 and dayC < (1 - criticalDes)*localH:
        status = -1
    sheet.cell(row = rowNo, column = 8).value = status
    print(status)
book.save('result.xlsx')
from openpyxl import load_workbook   #王薰珮指導小仙女修程式設計
book = load_workbook(filename='wb.xlsx')
print(book.sheetnames)  #列出所有工作表
#以下也被註解暫時不執行python語言註解
#後面不執行 前後三個引號的部分也不執行 都是註解用
sheet = book['王薰珮']     #工作表
sheet['A1'].value = '王薰珮python控制excel試算表'
rowBegin = 4                  #起始列
rowEnd = 10                  #結束列780先到10
criticalInc = 0.1
criticalDes = 0.1
localH = sheet.cell(row=rowBegin, column=3).value
localL = sheet.cell(row=rowBegin, column=4).value
status = 0
wealth = 0
for x, row in enumerate(sheet.iter_rows(min_row = rowBegin, max_row = rowEnd)):
    rowNo = x + rowBegin        #串列編號 x 起始於 0
    dayH = row[2].value         #當日最高C欄
    dayL = row[3].value         #當日最低D欄
    dayC = row[4].value         #當日收盤E欄
    if status == -1:            #檢測波段高
        localH = dayH
    elif localH < dayH:         
        localH = dayH
    if status == 1:             #檢測波段低
        localL = dayL
    elif localL > dayL:
        localL = dayL
    sheet.cell(row = rowNo, column = 6).value = localH #寫入波段高於F欄
    sheet.cell(row = rowNo, column = 7).value = localL #寫入波段低於G欄
    if status < 1 and dayC > (1 + criticalInc)*localL:
        status = 1
    elif status > -1 and dayC < (1 - criticalDes)*localH:
        status = -1
    sheet.cell(row = rowNo, column = 8).value = status
book.save('result.xlsx') #結果另存為result.xlsx


留言

張貼留言

這個網誌中的熱門文章

王薰珮python視窗使用者介面GUI類別class建構正多邊形或星形