1import xlsxwriter
2
3# Create a workbook and add a worksheet.
4workbook = xlsxwriter.Workbook('Expenses01.xlsx')
5worksheet = workbook.add_worksheet()
6
7# Some data we want to write to the worksheet.
8expenses = (
9 ['Rent', 1000],
10 ['Gas', 100],
11 ['Food', 300],
12 ['Gym', 50],
13)
14
15# Start from the first cell. Rows and columns are zero indexed.
16row = 0
17col = 0
18
19# Iterate over the data and write it out row by row.
20for item, cost in (expenses):
21 worksheet.write(row, col, item)
22 worksheet.write(row, col + 1, cost)
23 row += 1
24
25# Write a total using a formula.
26worksheet.write(row, 0, 'Total')
27worksheet.write(row, 1, '=SUM(B1:B4)')
28
29workbook.close()
30
1from openpyxl import Workbook
2
3filename = 'Testwriteexcel.xlsx'
4sheettitle = 'Sheetname'
5
6wb = Workbook()
7wb['Sheet'].title = sheettitle
8sh1 = wb.active
9sh1['A1'].value = 'Numbers'
10sh1['B1'].value = 'Sqares'
11sh1['C1'].value = 'Column 3'
12
13for n in range(1, 1+10):
14 sh1[f'A{1+n}'].value = n
15 sh1[f'B{1+n}'].value = n**2
16
17try:
18 wb.save(filename)
19except PermissionError:
20 print('File might be opened, please close it before writing')