python|太强了,用 Python+Excel 制作天气预报表!

今天给大家介绍一个Python+Excel的实战项目,非常有趣。 主要使用xlwings和requests这两个Python库,以及Office的Excel。xlwings处理表格,requests则是请求数据。 先从Excel中获取城市信息,然后请求接口,获取到天气信息,再返回给Excel。
具体操作可以看下图,文末提供进群技术讨论群,欢迎加入
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

在城市栏输入杭州,点击查询按钮,表格的数据就会发生变化,的确是杭州的天气预报。
① 数据获取
既然是天气预报,那肯定是需要天气数据的。
找了一圈国内开放的天气API接口,大多都是需要注册,果断放弃。
腾讯倒是有个不错的,可惜接口信息不太完整,没有相应的数据说明。
地址:https://tianqi.qq.com/
接口地址:https://wis.qq.com/weather/common
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

最终选择了一个国外的天气API接口。
地址:https://www.metaweather.com/zh/
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

并没有提供国内所有的城市,目前只有10个城市。
所以要想城市多一些,腾讯的天气接口还是可以考虑的。
一共是有10种天气状态,并且提供了相关的天气状态图片,可以供我们使用。
图片已经下载下来了,需要的小伙伴可以文末获取哦!
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

首先通过查询,获取城市的ID值。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

然后根据ID值,再去获取对应的天气信息。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

相关名称的中英文对照如下。

# 天气--中英文名对照 weather = {'Snow': '雪', 'Sleet': '雨夹雪', 'Hail': '冰雹', 'Thunderstorm': '雷阵雨', 'Heavy Rain': '大雨', 'Light Rain': '小雨', 'Showers': '阵雨', 'Heavy Cloud': '阴', 'Light Cloud': '多云', 'Clear': '晴' }# 城市--中英文名对照 citys = {'北京': 'Beijing', '成都': 'Chengdu', '东莞': 'Dongguan', '广州': 'Guangzhou', '杭州': 'Hangzhou', '香港': 'Hong Kong', '上海': 'Shanghai', '深圳': 'Shenzhen', '天津': 'Tianjin', '武汉': 'Wuhan' }

② 创建表格
安装xlwings库,并且使用命令行创建项目。
# 安装xlwings pip install xlwings -i https://mirror.baidu.com/pypi/simple/# 命令行运行 xlwings quickstart weatherapp --standalone

如此便会生成两个文件,Python和Excel文件。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

其中weatherapp.py的文件内容如下。
import xlwings as xwdef main(): wb = xw.Book.caller() sheet = wb.sheets[0] if sheet["A1"].value =https://www.it610.com/article/="Hello xlwings!": sheet["A1"].value = "https://www.it610.com/article/Bye xlwings!" else: sheet["A1"].value = "https://www.it610.com/article/Hello xlwings!"if __name__ == "__main__": xw.Book("weatherapp.xlsm").set_mock_caller() main()

而Excel是什么内容也没有的,打开时会提示是否启用宏,选择是。
然后需要将Excel的开发工具打开,后面会使用它插入一些元素。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

上图为Mac电脑的设置,Windows电脑设置起来也很简单,具体可以百度。
通过点击开发工具选项,我们可以使用Excle的Visual Basic 编辑器(VBA),还能插入按钮(查询按钮)。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

然后我在表格中插入一个点击按钮。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

选择宏名称为SampleCall,宏的位置为当前工作簿。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

点击按钮1,A1单元格出现内容Hello xlwings!。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

再次点击,A1单元格内容变为Bye xlwings!。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

也就意味着,修改weatherapp.py文件的代码,即可实现Excel的交互操作。
下面对表格进行页面设计,毕竟要让表格好看起来。
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

设置表格的行高、列宽、背景色、固定文字内容等信息。
将单元格C3名称设置为city_name,插入6张太阳图片,排列在单元格C9~H9处,居中对齐,图片也改名为no.1~no.6。
修改weatherapp.py文件代码如下。
import json from pathlib import Path import requests import xlwings as xw# 天气--中英文名对照 weather = {'Snow': '雪', 'Sleet': '雨夹雪', 'Hail': '冰雹', 'Thunderstorm': '雷阵雨', 'Heavy Rain': '大雨', 'Light Rain': '小雨', 'Showers': '阵雨', 'Heavy Cloud': '阴', 'Light Cloud': '多云', 'Clear': '晴' }# 城市--中英文名对照 citys = {'北京': 'Beijing', '成都': 'Chengdu', '东莞': 'Dongguan', '广州': 'Guangzhou', '杭州': 'Hangzhou', '香港': 'Hong Kong', '上海': 'Shanghai', '深圳': 'Shenzhen', '天津': 'Tianjin', '武汉': 'Wuhan' }def main(): # 通过runpython从excel中调用python函数 wb = xw.Book.caller() sht = wb.sheets[0]# 从Excel中读取城市信息 city_name = citys[sht.range("city_name").value]# 获取城市的ID值, 即woeid URL_CITY = f"https://www.metaweather.com/api/location/search/?query={ city_name}" response_city = requests.request("GET", URL_CITY) city_title = json.loads(response_city.text)[0]["title"] city_id = json.loads(response_city.text)[0]["woeid"]# 获取城市的天气信息 URL_WEATHER = f"https://www.metaweather.com/api/location/{ city_id}/" response_weather = requests.request("GET", URL_WEATHER) weather_data = https://www.it610.com/article/json.loads(response_weather.text)["consolidated_weather"]# 创建空列表, 存储数据 min_temp = [] max_temp = [] weather_state_name = [] weather_state_abbr = [] applicable_date = []# 处理数据 for index, day in enumerate(weather_data): # 最低温度 min_temp.append(weather_data[index]["min_temp"]) # 最高温度 max_temp.append(weather_data[index]["max_temp"]) # 天气情况 weather_state_name.append(weather[weather_data[index]["weather_state_name"]]) # 天气情况缩写 weather_state_abbr.append(weather_data[index]["weather_state_abbr"]) # 日期 applicable_date.append(weather_data[index]["applicable_date"])# 将获取到的值填充到Excel中 sht.range("C5").value = https://www.it610.com/article/applicable_date sht.range("C6").value = https://www.it610.com/article/weather_state_name sht.range("C7").value = https://www.it610.com/article/max_temp sht.range("C8").value = https://www.it610.com/article/min_temp sht.range("D3").value = https://www.it610.com/article/city_title# 创建列表 icon_names = ["no.1", "no.2", "no.3", "no.4", "no.5", "no.6"]# 设置天气图片路径 icon_path = Path(__file__).parent / "images"# 将天气情况与天气图片进行匹配,更新表格 for icon, abbr in zip(icon_names, weather_state_abbr): image_path = Path(icon_path, abbr + ".png") sht.pictures.add(image_path, name=icon, update=True)if __name__ == "__main__": # 设置用于调试caller()的excel文件,可以直接在python里运行 xw.Book("weatherapp.xlsm").set_mock_caller() main()

此时我们打开Excel表格,在城市栏输入10个城市中的一个,然后点击查询按钮,天气就会更新。

好了,本期的分享就到此结束了,有兴趣的小伙伴可以自行去实践学习。
技术交流 欢迎转载、收藏、有所收获点赞支持一下!
python|太强了,用 Python+Excel 制作天气预报表!
文章图片

目前开通了技术交流群,群友已超过2000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友
  • 方式①、发送如下图片至微信,长按识别,后台回复:加群;
  • 方式②、添加微信号:dkl88191,备注:来自CSDN
  • 方式③、微信搜索公众号:Python学习与数据挖掘,后台回复:加群
【python|太强了,用 Python+Excel 制作天气预报表!】python|太强了,用 Python+Excel 制作天气预报表!
文章图片

    推荐阅读