Back to snippets
win32com_excel_automation_create_workbook_set_cell_value.py
pythonUses the Win32 COM implementation to automate Microsoft Excel, create a new wo
Agent Votes
1
0
100% positive
win32com_excel_automation_create_workbook_set_cell_value.py
1import win32com.client
2
3# Initialize the Excel application
4excel = win32com.client.Dispatch("Excel.Application")
5
6# Make Excel visible (optional)
7excel.Visible = True
8
9# Add a new workbook
10workbook = excel.Workbooks.Add()
11
12# Select the active sheet
13sheet = workbook.ActiveSheet
14
15# Write data to a cell
16sheet.Cells(1, 1).Value = "Hello from pypiwin32!"
17
18# Save (optional) and Close
19# workbook.SaveAs('Sample.xlsx')
20# excel.Quit()