This code isn't really good and has multiple errors. For example, in CreateWorkbook, you create a workbook, and then save it. However, after, in CreateSheet, you don't actually, reopen the file. The file must be re opened after every save. This is how I would fix those errors:
import openpyxl
def CreateWorkbook(workbook_path):
workbook = openpyxl.Workbook()
workbook.save(workbook_path)
return workbook_path
def CreateSheet(workbook, sheet_name):
wb = openpyxl.load_workbook(workbook)
wb.create_sheet(sheet_name)
wb.save(workbook)
return sheet_name
def WriteCell(workbook, sheet_name, cell, cell_data):
wb = openpyxl.load_workbook(workbook)
worksheet = wb[sheet_name]
worksheet[cell] = cell_data
wb.save(workbook)
return
workbook = CreateWorkbook('workbook1.xlsx')
sheet = CreateSheet(workbook, 'Sheet1')
WriteCell(workbook, sheet, 'A1', 'testing')
I hope this works for you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…