# Save and close the workbook
wb.save()
wb.close()
#### Example: Creating an Automated Invoice System
python
def generate_invoice(customer_name, items):
wb = xw.Book("invoice_template.xlsx")
sheet = wb.sheets["Invoice"]
# Set customer name and date
sheet.range("B2").value = customer_name
sheet.range("B3").value = pd.Timestamp.now().strftime("%Y-%m-%d")
# Write item details to the invoice
for i, item in enumerate(items, start=6): # Starting from row 6
sheet.range(f"A{i}").value = item['description']
sheet.range(f"B{i}").value = item['quantity']
sheet.range(f"C{i}").value = item['unit_price']
sheet.range(f"D{i}").value = item['quantity'] * item['unit_price']
# Save and print the file
wb.save(f"{customer_name}_invoice.xlsx")
wb.app.quit()
# Sample items list
items = [
{"description": "Item 1", "quantity": 2, "unit_price": 50},
{"description": "Item 2", "quantity": 1, "unit_price": 100}
]
generate_invoice("John Doe", items)
`