python 列表写入excel_[python]获取一年日历数据并写入excel表格中
[Asm] 純文本查看 復制代碼# coding=gbk
import requests
from bs4 import BeautifulSoup
import xlwt
# 獲取一年數(shù)據(jù),以字典返回
def getYear():
yearDic = {}
week = 2 # 初始星期,2019年1月1日為星期二
year = 2019 # 年份 想要哪年的就改成那年的 記得修改上面的初始星期
urlList = []
uUrl = 'https://wannianrili.51240.com/ajax/?q={}-{}&v=18121803'
# 構造每個月的接口鏈接
for y in range(1,13):
if y<10:
rUrl = uUrl.format(year,'0'+str(y))
urlList.append(rUrl)
else:
rUrl = uUrl.format(year,y)
urlList.append(rUrl)
headers = {
'User-Agent': 'Mozilla / 5.0(Windows NT 10.0; Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 68.0.3440.106Safari / 537.36',
}
for i in range(0,len(urlList)):
monthDic = {}
html = requests.get(urlList[i], headers=headers)
soup = BeautifulSoup(html.content, 'lxml')
riqiList = soup.find_all(class_='wnrl_riqi')
for riqi in riqiList:
dayList = []
g = riqi.find_all(class_='wnrl_td_gl')
n = riqi.find_all(class_='wnrl_td_bzl')
gStr = g[0].get_text()
nStr = n[0].get_text()
# 找到該元素視為法定節(jié)假日
if riqi.find_all(class_='wnrl_riqi_xiu'):
nStr+='(休)'
dayList.append(week)
# 到星期日后重置為0
if week == 7:
week = 0
week+=1
dayList.append(gStr)
dayList.append(nStr)
monthDic[gStr] = dayList
yearDic[i+1] = monthDic
return yearDic,year
# 初始每個月的星期標題單元格坐標,頁面3*4網(wǎng)格方式展示
def coordinates():
yearCoorDic = {}
x = 2 # 初始橫坐標
y = 0 # 初始縱坐標
interval = 2 # 月份之間的縱坐標間距
for i in range(1,13):
monthList = []
# 月份為1,5,9 時num重新初始為0
if i == 1 or i == 5 or i == 9:
num = y
if i < 5:
# 循環(huán)7次,為星期一到星期天
for k in range(7):
tList = []
# cross = x # 橫
# 每次縱坐標+1
column = k + num # 縱
tList.append(x)
tList.append(column)
# 記住最后一次(星期天)縱坐標+月份間隔,為下個月(星期一)的縱坐標值
if k == 6:
num = column+interval
monthList.append(tList)
if i>4 and i<9:
for k in range(7):
tList = []
# 橫坐標方向的單元格數(shù),計算得出日+農(nóng)歷最大占用12行。這里給14行算上橫坐標間距
cross = x + 14 # 橫
column = k + num # 縱
tList.append(cross)
tList.append(column)
if k == 6:
num = column+interval
monthList.append(tList)
if i>8 and i<13:
for k in range(7):
tList = []
cross = x + 14*2 # 橫
column = k + num # 縱
tList.append(cross)
tList.append(column)
if k == 6:
num = column+interval
monthList.append(tList)
yearCoorDic[i] = monthList
return yearCoorDic
def template():
book = xlwt.Workbook() # 新建一個excel對象
sheet = book.add_sheet('2019年日歷表') # 添加一個sheet頁
month_style = xlwt.easyxf('font: height 280;') # 定義月份標題單元格高度
week_style = xlwt.easyxf('font: height 340;') # 定義星期單元格高度
Content_style = xlwt.easyxf('font: height 280;') # 定義日期和農(nóng)歷單元格高度
styleRed = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于周末單元格
styleRed1 = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于周末單元格且節(jié)日名過長時
styleBlack = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于工作日單元格
styleBlack_ = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于農(nóng)歷單元格
styleBlack1_ = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于農(nóng)歷單元格且節(jié)日名過長時
titleStyle = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于月份標題單元格
styleContent = xlwt.XFStyle() # 創(chuàng)建一個樣式對象,初始化樣式 適用于日期和農(nóng)歷單元格
# 設置單元格樣式 通用
al = xlwt.Alignment()
al.horz = 0x02 # 設置水平居中
al.vert = 0x01 # 設置垂直居中
# 設置單元格樣式 適用于styleRed1 styleBlack1_
al1 = xlwt.Alignment()
al1.vert = 0x01 # 設置垂直居中
# 設置單元格樣式 適用于周末和法定節(jié)假日
fnt = xlwt.Font()
fnt.bold = True # 字體加粗
fnt.name = u'微軟雅黑' # 設置其字體為微軟雅黑
fnt.colour_index = 2 # 字體紅色
# 設置單元格樣式 適用于工作日和星期
fnt1 = xlwt.Font()
fnt1.bold = True # 字體加粗
fnt1.name = u'微軟雅黑' # 設置其字體為微軟雅黑
fnt1.colour_index = 0 # 字體黑色
# 設置單元格樣式 適用于農(nóng)歷
fnt1_ = xlwt.Font()
fnt1_.bold = True # 字體加粗
fnt1_.name = u'微軟雅黑' # 設置其字體為微軟雅黑
fnt1_.colour_index = 23 # 字體灰色
# 設置單元格樣式 適用于月份標題顯示
fnt2 = xlwt.Font()
fnt2.bold = True # 字體加粗
fnt2.name = u'微軟雅黑' # 設置其字體為微軟雅黑
fnt2.colour_index = 1 # 字體黑色
pattern = xlwt.Pattern() # Create the pattern
pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
pattern.pattern_fore_colour = 8 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on..
# 應用單元格樣式
styleRed.alignment = al
styleRed.font = fnt
styleRed1.alignment = al1
styleRed1.font = fnt
# 應用單元格樣式
styleBlack.alignment = al
styleBlack.font = fnt1
# 應用單元格樣式
styleBlack_.alignment = al
styleBlack_.font = fnt1_
styleBlack1_.alignment = al1
styleBlack1_.font = fnt1_
# 應用單元格樣式
titleStyle.alignment = al
titleStyle.font = fnt2
titleStyle.pattern = pattern
styleContent.alignment = al
# 獲取每個月星期標題坐標初始值
yearCoorDic = coordinates()
print('正在獲取日歷數(shù)據(jù)...')
# 獲取一年的數(shù)據(jù)
yearDic,year = getYear()
titList = list(yearCoorDic.keys())
print('%s年數(shù)據(jù)獲取成功,正在寫入模板...'%year)
for i in range(len(titList)): # 12個月份
# 獲取第一個月份的星期初始坐標
titcoorList = yearCoorDic[titList[i]]
# 設置應用月份標題的高度
first_row = sheet.row(titcoorList[0][0]-1)
first_row.set_style(month_style)
sheet.write_merge(titcoorList[0][0]-1, titcoorList[0][0]-1, titcoorList[0][1], titcoorList[-1][1], '{}月'.format(i+1), titleStyle) # 合并單元格并寫入月份
# 根據(jù)坐標寫入星期標題
for j in range(0,len(titcoorList)):
# 設置應用星期標題寬度
first_col = sheet.col(titcoorList[j][1])
first_col.width = 95 * 25
# 設置應用星期標題的高度
first_row = sheet.row(titcoorList[j][0])
first_row.set_style(week_style)
if j+1 == 1:
title = '星期一'
elif j+1 == 2:
title = '星期二'
elif j+1 == 3:
title = '星期三'
elif j+1 == 4:
title = '星期四'
elif j+1 == 5:
title = '星期五'
elif j+1 == 6:
title = '星期六'
else:
title = '星期日'
sheet.write(titcoorList[j][0], titcoorList[j][1], title , styleBlack if j+1<6 else styleRed)
# 初始每個星期橫坐標初始值
oneNum,twoNum,threeNum,fourNum,fiveNum,sixNum,sevenNum = 1,1,1,1,1,1,1 # 總覺得有簡化的方法 這樣寫感覺好傻-.-
# 獲取第一個月份數(shù)據(jù)的鍵值列表
daykeyList = list(yearDic[titList[i]].keys())
for k in range(len(daykeyList)): # 每個月的日期
dayList = yearDic[titList[i]][daykeyList[k]] # 獲取每日的列表值['2','01','元旦'] 第一個值為星期幾,第二個為日期,第三個為農(nóng)歷或節(jié)假日
# 判斷每個月第一天為星期幾,若為星期二,則把星期一的橫坐標初始值初始為3,即星期一為空,以此類推
if k == 0:
if dayList[0]==2:
oneNum = 3
if dayList[0]==3:
oneNum,twoNum = 3,3
if dayList[0]==4:
oneNum,twoNum,threeNum = 3,3,3
if dayList[0]==5:
oneNum,twoNum,threeNum,fourNum= 3,3,3,3
if dayList[0]==6:
oneNum,twoNum,threeNum,fourNum,fiveNum= 3,3,3,3,3
if dayList[0]==7:
oneNum,twoNum,threeNum,fourNum,fiveNum,sixNum= 3,3,3,3,3,3
# 判斷日期是星期幾,執(zhí)行對應語句
if 1 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[0][0] + oneNum)
first_row.set_style(Content_style)
# 寫入日期,日期橫坐標的值為初始星期標題坐標值+oneNum初始值
sheet.write(titcoorList[0][0] + oneNum, titcoorList[0][1], dayList[1], styleRed if '休' in dayList[2] else styleBlack)
# 初始值+1 為下個農(nóng)歷節(jié)假日的初始值
oneNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[0][0] + oneNum)
first_row.set_style(Content_style)
# 寫入農(nóng)歷或節(jié)假日 初始星期標題+oneNum初始值
sheet.write(titcoorList[0][0] + oneNum, titcoorList[0][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
oneNum+=1
if 2 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[1][0] + twoNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[1][0] + twoNum, titcoorList[1][1], dayList[1], styleRed if '休' in dayList[2] else styleBlack)
twoNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[1][0] + twoNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[1][0] + twoNum, titcoorList[1][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
twoNum+=1
if 3 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[2][0] + threeNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[2][0] + threeNum, titcoorList[2][1], dayList[1], styleRed if '休' in dayList[2] else styleBlack)
threeNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[2][0] + threeNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[2][0] + threeNum, titcoorList[2][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
threeNum+=1
if 4 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[3][0] + fourNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[3][0] + fourNum, titcoorList[3][1], dayList[1], styleRed if '休' in dayList[2] else styleBlack)
fourNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[3][0] + fourNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[3][0] + fourNum, titcoorList[3][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
fourNum+=1
if 5 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[4][0] + fiveNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[4][0] + fiveNum, titcoorList[4][1], dayList[1], styleRed if '休' in dayList[2] else styleBlack)
fiveNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[4][0] + fiveNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[4][0] + fiveNum, titcoorList[4][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
fiveNum+=1
if 6 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[5][0] + sixNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[5][0] + sixNum, titcoorList[5][1], dayList[1], styleRed)
sixNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[5][0] + sixNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[5][0] + sixNum, titcoorList[5][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
sixNum+=1
if 7 == dayList[0]:
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[6][0] + sevenNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[6][0] + sevenNum, titcoorList[6][1], dayList[1], styleRed)
sevenNum+=1
# 設置應用單元格的高度
first_row = sheet.row(titcoorList[6][0] + sevenNum)
first_row.set_style(Content_style)
sheet.write(titcoorList[6][0] + sevenNum, titcoorList[6][1], dayList[2], styleRed if '休' in dayList[2] else styleBlack1_ if len(dayList[2])>4 else styleBlack_)
sevenNum+=1
book.save('%s年日歷表.xls'%year)
print('保存成功,程序執(zhí)行完畢...')
template()
總結
以上是生活随笔為你收集整理的python 列表写入excel_[python]获取一年日历数据并写入excel表格中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 荧光定量 PCR 要点解析
- 下一篇: 对《基于机器学习的区域滑坡危险性评价方法