python通過openpyxl讀寫excel數據

公交數據

1、

#用openpyxl讀取excel數據,要事先安裝pip install openpyxl
import openpyxl
workbook=openpyxl.load_workbook('gongjiao.xlsx')
sheet=workbook.get_sheet_by_name('Sheet1') #獲取第一個sheet
max_row=sheet.max_row #最大行
print(max_row)
max_col=sheet.max_column #最大列
print(max_col)

2、

#space_L空間中有值的填1
from openpyxl import Workbook
wb = Workbook()
# 切換到活動工作表
ws = wb.active

for i in range(1,max_row+1):
    for j in range(1,max_col+1):
        cell_i_j=sheet.cell(row=i,column=j).value
        cell_i_j_1=sheet.cell(row=i,column=j+1).value
       
        if (cell_i_j!=0 and cell_i_j_1!=0) and cell_i_j_1!=None:
           # print(cell_i_j,cell_i_j_1)
            ws.cell(row=cell_i_j,column=cell_i_j_1).value=1
            ws.cell(row=cell_i_j_1,column=cell_i_j).value=1
wb.save('gongjiao_l.xlsx')

3、#space_L空間中沒值的填0
import openpyxl
workbook=openpyxl.load_workbook('gongjiao_l.xlsx')
sheet=workbook.get_sheet_by_name('Sheet') #獲取第一個sheet
max_row=sheet.max_row #最大行
print(max_row)
max_col=sheet.max_column #最大列
print(max_col)
for i in range(1,max_row+1):
    for j in range(1,max_col+1):
        #print(sheet.cell(row=i,column=j).value)
        if sheet.cell(row=i,column=j).value!=1:
            sheet.cell(row=i,column=j).value=0
workbook.save('gongjiao_l.xlsx')

4、

#用openpyxl讀取excel數據,要事先安裝pip install openpyxl
import openpyxl
workbook=openpyxl.load_workbook('gongjiao.xlsx')
sheet=workbook.get_sheet_by_name('Sheet1') #獲取第一個sheet
max_row=sheet.max_row #最大行
print(max_row)
max_col=sheet.max_column #最大列
print(max_col)

5、

#space_p空間有值的填1
from openpyxl import Workbook
wb = Workbook()
# 切換到活動工作表
ws = wb.active
for i in range(1,max_row+1):
    for j in range(1,max_col+1):
        for k in range(j+1,max_col+1):

            cell_i_j=sheet.cell(row=i,column=j).value
            cell_i_j_1=sheet.cell(row=i,column=k).value
#            print(cell_i_j,cell_i_j_1)

            if (cell_i_j!=0 and cell_i_j_1!=0) and cell_i_j_1!=None:
           # print(cell_i_j,cell_i_j_1)
                ws.cell(row=cell_i_j,column=cell_i_j_1).value=1
                ws.cell(row=cell_i_j_1,column=cell_i_j).value=1
wb.save('gongjiao_p.xlsx')

6、

#space_P空間無值的填0
import openpyxl
workbook=openpyxl.load_workbook('gongjiao_p.xlsx')
sheet=workbook.get_sheet_by_name('Sheet') #獲取第一個sheet
max_row=sheet.max_row #最大行
print(max_row)
max_col=sheet.max_column #最大列
print(max_col)
for i in range(1,max_row+1):
    for j in range(1,max_col+1):
        #print(sheet.cell(row=i,column=j).value)
        if sheet.cell(row=i,column=j).value!=1:
            sheet.cell(row=i,column=j).value=0
workbook.save('gongjiao_p.xlsx')
#已形成公交換乘網絡

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章