python代码用openpyxl库读写excel文件

#To create a workbook, you can import the Workbook class and start work: 
from openpyxl import Workbook; 
wb = Workbook()
#To grab the active worksheet, you can use 
ws = wb.active
#To assign data directly to cells, you can use 
ws['A1'] = 42.
#To append rows, you can use 
ws.append([1, 2, 3])
ws.append([6, 7, 8])
ws.append([1*2, 2*2, 3*2])
ws.append([6*3, 7*3, 8*4])

#To add formulas using the openpyxl module just like you add values to a cell, you can use 
ws['A9'] = '=SUM(A1:A8)'
wb.save(r"d:\myexcel.xlsx")

#Read .xlsx wenjan 
# import openpyxl 
# worksheet = openpyxl.load_workbook(r"d:\myexcel.xlsx") 
# sheet = worksheet.active 
# for row in sheet.iter_rows(min_row=1, min_col=1, max_row=6, max_col=2): 
#     for cell in row: 
#         print(cell.value, end=" ") 
#     print()

#To read excel.xlsx File
import openpyxl

# Load the workbook
workbook = openpyxl.load_workbook(r"d:\myexcel.xlsx")

# Select the worksheet
worksheet = workbook.active

# # Access a cell value
# cell = worksheet.cell(row=1, column=1)
# print(cell.value)

# Iterate through rows and columns
for row in worksheet.iter_rows(min_row=1, max_col=3):
    for cell in row:
        print(cell.value,end= " ")
    print()