读取xlsx格式
wb = openpyxl.load_workbook(filename, data_only=True)
workbook = openpyxl.load_workbook(input_file)
sheet_matrix = workbook["Matrix"]
print("最大列数",sheet_matrix.max_column)
print("最大行数",sheet_matrix.max_row)
print("单元格的值",sheet_matrix.cell(1,2).value)#索引值从1开始
def read_excel_xlsx(path, sheet_name):
workbook = openpyxl.load_workbook(path)
sheet = workbook[sheet_name]
for row in sheet.rows:
for cell in row:
print(cell.value, "\t", end="")
print()
写xlsx格式表格
def write_excel_xlsx(path, sheet_name, value):
index = len(value)
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = sheet_name
for i in range(0, index):
for j in range(0, len(value[i])):
sheet.cell(row=j+1, column=i+1, value=str(value[i][j]))
workbook.save(path)
print("xlsx格式表格写入数据成功!")
##########
def main():
book_name_xlsx = 'xlsx格式测试工作簿.xlsx'
sheet_name_xlsx = 'xlsx格式测试表'
value3 = [["第一列", "第一列", "第一列", "第一列", "第一列"],
["第二列", "女", "gg", "家庄", "程师"],
["第三列", "男", "gg", "京", "老板"],
["第四列", "女", "gg", "州", "保安"]]
write_excel_xlsx(book_name_xlsx, sheet_name_xlsx, value3)
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = ''
sheet2=workbook.create_sheet('')
sheet_row,sheet2_row=1,1
for i in range(1, len()+1):
if '' in :
sheet2.cell(row=sheet2_row, column=2, value='')
sheet2_row=sheet2_row+1
else:
sheet.cell(row=sheet_row, column=2, value='')
sheet_row=sheet_row+1
workbook.save('.xlsx')