成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Pythonxlwt導(dǎo)出excel完整版-創(chuàng)新互聯(lián)

有一組任務(wù)數(shù)據(jù),要把它excel下載下來,如果僅僅導(dǎo)出,用csv導(dǎo)出就很方便。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了甕安免費建站歡迎大家使用!

但是要導(dǎo)出漂亮的樣式,重復(fù)的地方要合并單元格,設(shè)置背影顏色,字體,邊框等。而CSV是純文本格式,不支持設(shè)置各種樣式。

研究了一天,把代碼寫了出來。

背影顏色參考地址,搜索_colour_map_text

樣式設(shè)置參考地址

完整代碼如下:


import xlwt

book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('sheetname')

def set_style(color='white', height=260, bold=False):

    #: 設(shè)置單元格字體
    style = xlwt.XFStyle()
    font = xlwt.Font()
    # font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font

    # 設(shè)置單元格背景顏色
    pattern = xlwt.Pattern()
    pattern.pattern = xlwt.Pattern.SOLID_PATTERN
    pattern.pattern_fore_colour = xlwt.Style.colour_map[color]
    style.pattern = pattern

    #:設(shè)置單元格邊框線條
    borders = xlwt.Borders()  # Create Borders
    # May be: NO_LINE, THIN, MEDIUM, DASHED, DOTTED, THICK, DOUBLE, HAIR,
    # MEDIUM_DASHED, THIN_DASH_DOTTED, MEDIUM_DASH_DOTTED,
    # THIN_DASH_DOT_DOTTED, MEDIUM_DASH_DOT_DOTTED,
    # SLANTED_MEDIUM_DASH_DOTTED, or 0x00 through 0x0D.
    borders.left = xlwt.Borders.THIN
    borders.right = xlwt.Borders.THIN
    borders.top = xlwt.Borders.THIN
    borders.bottom = xlwt.Borders.THIN
    style.borders = borders  # Add Borders to Style

    #: 設(shè)置單元格居中格式
    alignment = xlwt.Alignment()  # Create Alignment
    # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED,
    # HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
    alignment.horz = xlwt.Alignment.HORZ_CENTER  # 橫向居中
    alignment.vert = xlwt.Alignment.VERT_CENTER  # 縱向居中  May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
    alignment.wrap = xlwt.Alignment.WRAP_AT_RIGHT  # :自動換行
    style.alignment = alignment
    return style

#:模擬數(shù)據(jù)
data = [
    {
        "goal_id": 17,
        "goal_name": "\u5b9e\u73b0OKR\u4e0e\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\u4e0a\u7ebf\u4e14\u672c\u7ec4\u5185\u65e5\u5e38\u5316\u4f7f\u7528",
        "goal_progress": 0,
        "name": "\u589e\u52a0\u76ee\u6807\u3001\u4efb\u52a1\u7684\u5ef6\u671f\u63d0\u9192\u4e0e\u5c55\u793a\u3002",
        "owner": "aaron.yang",
        "progress": 0,
        "result_id": 26,
        "result_name": "\u5b8c\u6210\u4efb\u52a1\u3001OKR\u3001\u62a5\u8868\u3001\u7ee9\u6548\u529f\u80fd\u6a21\u5757\u5f00\u53d1\u4e0e\u4e0a\u7ebf",
        "result_progress": 0,
    },
    {
        "goal_id": 17,
        "goal_name": "\u5b9e\u73b0OKR\u4e0e\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\u4e0a\u7ebf\u4e14\u672c\u7ec4\u5185\u65e5\u5e38\u5316\u4f7f\u7528",
        "goal_progress": 0,
        "name": "task2",
        "owner": "aaron.yang",
        "progress": 0,
        "result_id": 26,
        "result_name": "\u5b8c\u6210\u4efb\u52a1\u3001OKR\u3001\u62a5\u8868\u3001\u7ee9\u6548\u529f\u80fd\u6a21\u5757\u5f00\u53d1\u4e0e\u4e0a\u7ebf",
        "result_progress": 0,
    },
    {
        "goal_id": 17,
        "goal_name": "\u5b9e\u73b0OKR\u4e0e\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\u4e0a\u7ebf\u4e14\u672c\u7ec4\u5185\u65e5\u5e38\u5316\u4f7f\u7528",
        "goal_progress": 0,
        "name": "task3",
        "owner": "aaron.yang",
        "progress": 0,
        "result_id": 26,
        "result_name": "\u5b8c\u6210\u4efb\u52a1\u3001OKR\u3001\u62a5\u8868\u3001\u7ee9\u6548\u529f\u80fd\u6a21\u5757\u5f00\u53d1\u4e0e\u4e0a\u7ebf",
        "result_progress": 0,
    },
]
# 寫入第一行數(shù)據(jù)
row0 = ['目標名稱', '目標進度', '關(guān)鍵結(jié)果名稱', '關(guān)鍵結(jié)果進度', '任務(wù)名稱', '任務(wù)負責人', '任務(wù)進度']
sheet.write_merge(0, 0, 0, 3, 'OKR', set_style('purple_ega', height=500))
sheet.write_merge(0, 0, 4, 6, '任務(wù)', set_style('cyan_ega', height=500))

# 寫入第二行數(shù)據(jù)
for i in range(0, len(row0)):
    sheet.write(1, i, row0[i], set_style('sky_blue'))

# 把需要合并的單元格,統(tǒng)計要合并多少行,以key,value形式緩存起來。
goal_tmp = {}
result_tmp = {}
for task in data:
    if task['goal_id'] not in goal_tmp:
        goal_tmp[task['goal_id']] = 1
    else:
        goal_tmp[task['goal_id']] += 1

    if task['result_id'] not in result_tmp:
        result_tmp[task['result_id']] = 1
    else:
        result_tmp[task['result_id']] += 1

goal_ids = []
result_ids = []
for index, task in enumerate(data):
    index = index + 1
    row_start = 1 + index
    #: 如果已合并單元格,把id追加到列表,避免重復(fù)合并
    if task['goal_id'] not in goal_ids:
        row_end = index + goal_tmp[task['goal_id']]
        sheet.write_merge(row_start, row_end, 0, 0, task['goal_name'], set_style())
        sheet.write_merge(row_start, row_end, 1, 1, task['goal_progress'], set_style())
        goal_ids.append(task['goal_id'])
    if task['result_id'] not in result_ids:
        row_end = index + result_tmp[task['result_id']]
        sheet.write_merge(row_start, row_end, 2, 2, task['result_name'], set_style())
        sheet.write_merge(row_start, row_end, 3, 3, task['result_progress'], set_style('yellow'))
        result_ids.append(task['result_id'])

    # 寫入接下來的幾列數(shù)據(jù),并根據(jù)關(guān)鍵的列設(shè)置背景顏色
    sheet.write(row_start, 4, task['name'], set_style())
    sheet.write(row_start, 5, task['owner'], set_style())
    sheet.write(row_start, 6, task['progress'], set_style('yellow'))

#: 設(shè)置單元格寬度
sheet.col(0).width = 10000
sheet.col(2).width = 10000
sheet.col(3).width = 4000
sheet.col(4).width = 10000
sheet.col(5).width = 5000
#:寫入到文件
book.save('/tmp/bookname.xls')

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站標題:Pythonxlwt導(dǎo)出excel完整版-創(chuàng)新互聯(lián)
URL鏈接:http://jinyejixie.com/article6/depiig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、ChatGPT網(wǎng)站營銷、服務(wù)器托管網(wǎng)頁設(shè)計公司、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
棋牌| 雅江县| 永康市| 太白县| 德令哈市| 徐水县| 徐州市| 镇康县| 囊谦县| 闻喜县| 闻喜县| 囊谦县| 泾阳县| 新密市| 锡林郭勒盟| 芦山县| 栖霞市| 大安市| 河池市| 宁国市| 彭山县| 宁城县| 卫辉市| 保德县| 安福县| 德江县| 丰台区| 茶陵县| 黄石市| 古交市| 广德县| 淮北市| 西峡县| 岑溪市| 桃园县| 且末县| 昌都县| 阿尔山市| 嘉荫县| 古交市| 漠河县|