After running xlwings quickstart file_name
, the generated file has the following contents:
import xlwings as xw
@xw.sub # only required if you want to import it or run it via UDF Server
def main():
wb = xw.Book.caller()
wb.sheets[0].range("A1").value = "Hello xlwings!"
@xw.func
def hello(name):
return "hello {0}".format(name)
if __name__ == "__main__":
xw.books.active.set_mock_caller()
main()
After solving Exercise 2 and 3, the Python file could look like this:
# Standard library imports
import os
# Third party imports (dependencies)
import xlwings as xw
import pandas as pd
import matplotlib.pyplot as plt
@xw.sub # only required if you want to import it or run it via UDF Server
def main():
wb = xw.Book.caller()
wb.sheets[0].range("A1").value = "Hello xlwings!"
# UDF function that was generated here has been deleted (not used)
# -----------------------------------------------------------------------------
# EXERCISE 3
# ----------
def create_daterange_dataframe(filename, start_date, end_date):
'''Helper function'''
# Get the 'correct' current working dir (where the .py and .xlsm files are)
cwd = os.path.dirname(os.path.abspath(__file__))
# Combine into a full path to the dataset file
full_filename = f'{cwd}\\{filename}'
df_raw = pd.read_csv(full_filename)
# Change the "raw" dates into datetime objects so they can be filtered
df_raw['Date'] = pd.to_datetime(df_raw['Date'])
# Set the date as the index of the dataframe
df = df_raw.set_index('Date')
# Filter dataframe for input date range and return it
return df[start_date:end_date]
@xw.sub
def update_weather_plot():
'''Macro function to interact with Excel'''
# Mock the calling Excel file
xw.books.active.set_mock_caller()
# Get the workbook that is calling the macro and set sheet
wb = xw.Book.caller()
sht3 = wb.sheets['Sheet3']
# Get input values from Excel
start_date = sht3.range('C21').value
end_date = sht3.range('C22').value
parameter = sht3.range('C23').value
fig, ax = plt.subplots()
filename = 'Sydney_weather.csv'
# Create a dataframe from the dataset and filter it based on inputs
df = create_daterange_dataframe(filename, start_date, end_date)
# Use the Pandas interface to matplotlib for plotting
df.plot(y=parameter, ax=ax)
# Insert figure into sheet
sht3.pictures.add(fig, name='MyPlot', update=True)
# -----------------------------------------------------------------------------
if __name__ == "__main__":
xw.books.active.set_mock_caller()
main()
# -------------------------------------------------------------------------
# EXERCISE 2
# ----------
import numpy as np
# Establish connection to the workbook
wb = xw.Book.caller()
sht2 = wb.sheets['Sheet2']
sht2.range('A2').options(transpose=True).value = np.arange(-50, 51, 5)
# -------------------------------------------------------------------------
The look of the resulting Excel interface could be like shown below:
pd.describe
¶dfd = pd.read_csv('Sydney_weather.csv')
dfd.describe()
MinTemp | MaxTemp | Rainfall | Evaporation | Sunshine | WindGustSpeed | WindSpeed9am | WindSpeed3pm | Humidity9am | Humidity3pm | Pressure9am | Pressure3pm | Cloud9am | Cloud3pm | Temp9am | Temp3pm | RISK_MM | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 3334.000000 | 3335.000000 | 3331.000000 | 3286.000000 | 3321.000000 | 2301.000000 | 3311.000000 | 3312.000000 | 3323.000000 | 3324.000000 | 3317.000000 | 3318.000000 | 2771.000000 | 2776.000000 | 3333.000000 | 3333.000000 | 3337.000000 |
mean | 14.865057 | 23.002339 | 3.330231 | 5.187432 | 7.179374 | 41.761408 | 15.070674 | 19.335447 | 68.229010 | 54.699158 | 1018.346156 | 1016.018774 | 4.181523 | 4.218660 | 17.819742 | 21.533333 | 3.319868 |
std | 4.553641 | 4.494638 | 9.895172 | 2.777407 | 3.810886 | 13.007523 | 7.079724 | 7.482554 | 15.085355 | 16.293530 | 7.021571 | 7.032211 | 2.749578 | 2.641885 | 4.897177 | 4.303737 | 9.885066 |
min | 4.300000 | 11.700000 | 0.000000 | 0.000000 | 0.000000 | 17.000000 | 0.000000 | 0.000000 | 19.000000 | 10.000000 | 986.700000 | 989.800000 | 0.000000 | 0.000000 | 6.400000 | 10.200000 | 0.000000 |
25% | 11.000000 | 19.600000 | 0.000000 | 3.200000 | 4.300000 | 31.000000 | 9.000000 | 15.000000 | 58.000000 | 44.000000 | 1013.700000 | 1011.300000 | 1.000000 | 1.000000 | 13.800000 | 18.400000 | 0.000000 |
50% | 14.900000 | 22.800000 | 0.000000 | 4.800000 | 8.300000 | 41.000000 | 15.000000 | 19.000000 | 69.000000 | 56.000000 | 1018.600000 | 1016.300000 | 5.000000 | 4.000000 | 18.200000 | 21.300000 | 0.000000 |
75% | 18.700000 | 26.000000 | 1.400000 | 7.000000 | 10.200000 | 50.000000 | 20.000000 | 24.000000 | 80.000000 | 65.000000 | 1023.100000 | 1020.800000 | 7.000000 | 7.000000 | 21.700000 | 24.500000 | 1.400000 |
max | 27.600000 | 45.800000 | 119.400000 | 18.400000 | 13.600000 | 96.000000 | 54.000000 | 57.000000 | 100.000000 | 99.000000 | 1039.000000 | 1036.700000 | 9.000000 | 8.000000 | 36.500000 | 44.700000 | 119.400000 |
The cell below is for setting the style of this document. It's not part of the exercises.
# Apply css theme to notebook
from IPython.display import HTML
HTML('<style>{}</style>'.format(open('../css/cowi.css').read()))