欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python xlrd/xlwt 創(chuàng)建excel文件及常用操作

 更新時(shí)間:2020年09月24日 09:21:38   作者:淡懷  
這篇文章主要介紹了Python xlrd/xlwt 創(chuàng)建excel文件及常用操作,幫助大家更好的理解和使用python辦公,感興趣的朋友可以了解下

一、創(chuàng)建excel代碼

備注:封裝好了(可直接調(diào)用)

"""
-*- coding:utf-8 -*-
@Time :2020/8/20 21:02
@Author :Jarvis
@File :jar_excel_util.py
@Version:1.0
"""
from typing import List

import xlwt


class JarExcelUtil:
 def __init__(self, header_list: List[list]):
  """
  :param header_list: 如下格式
   例1:默認(rèn)列寬
   header_list = [
    ['序號(hào)'], # 表格第0列[此列表頭名稱]
    ['姓名'],
    ['性別'],
    ['愛好'],
    ['生日']
   ]
   例2:自定義列寬(列寬值為int類型 英文字符長度 如:10 表示列寬為10個(gè)英文字符長度)
   header = [
    ['序號(hào)', 5], # 表格第0列[此列表頭名稱,列寬]
    ['姓名', 10], # 表格第1列[此列表頭名稱,列寬]
    ['性別', 10],
    ['愛好', 10],
    ['生日', 20]
   ]
  """
  self.data = header_list
  self.__color_str = 'aqua 0x31\r\n\
black 0x08\r\n\
blue 0x0C\r\n\
blue_gray 0x36\r\n\
bright_green 0x0B\r\n\
brown 0x3C\r\n\
coral 0x1D\r\n\
cyan_ega 0x0F\r\n\
dark_blue 0x12\r\n\
dark_blue_ega 0x12\r\n\
dark_green 0x3A\r\n\
dark_green_ega 0x11\r\n\
dark_purple 0x1C\r\n\
dark_red 0x10\r\n\
dark_red_ega 0x10\r\n\
dark_teal 0x38\r\n\
dark_yellow 0x13\r\n\
gold 0x33\r\n\
gray_ega 0x17\r\n\
gray25 0x16\r\n\
gray40 0x37\r\n\
gray50 0x17\r\n\
gray80 0x3F\r\n\
green 0x11\r\n\
ice_blue 0x1F\r\n\
indigo 0x3E\r\n\
ivory 0x1A\r\n\
lavender 0x2E\r\n\
light_blue 0x30\r\n\
light_green 0x2A\r\n\
light_orange 0x34\r\n\
light_turquoise 0x29\r\n\
light_yellow 0x2B\r\n\
lime 0x32\r\n\
magenta_ega 0x0E\r\n\
ocean_blue 0x1E\r\n\
olive_ega 0x13\r\n\
olive_green 0x3B\r\n\
orange 0x35\r\n\
pale_blue 0x2C\r\n\
periwinkle 0x18\r\n\
pink 0x0E\r\n\
plum 0x3D\r\n\
purple_ega 0x14\r\n\
red 0x0A\r\n\
rose 0x2D\r\n\
sea_green 0x39\r\n\
silver_ega 0x16\r\n\
sky_blue 0x28\r\n\
tan 0x2F\r\n\
teal 0x15\r\n\
teal_ega 0x15\r\n\
turquoise 0x0F\r\n\
violet 0x14\r\n\
white 0x09\r\n\
yellow 0x0D'
  self.color_list = [] # [[]] [['aqua', '0x31'], ['black', '0x08'], ...]
  for color in self.__color_str.split('\r\n'):
   color = color.split(' ')
   self.color_list.append(color)

 def write(self, out_file, data_body: List[list], sheet_name='sheet', frozen_row: int = 1, frozen_col: int = 0):
  """
  寫入數(shù)據(jù)
  :param out_file: 保存文件(如:test.xlsx)
  :param data_body: data_body[0]為表格第0行數(shù)據(jù) data_body[0][0]為表格第0行第0列單元格值
  :param sheet_name:
  :param frozen_row: 凍結(jié)行(默認(rèn)首行)
  :param frozen_col: 凍結(jié)列(默認(rèn)不凍結(jié))
  """
  # step1 判斷數(shù)據(jù)正確性(每行列數(shù)是否與表頭相同)
  count = 0
  for pro in data_body:
   if len(pro) != len(self.data):
    raise Exception(
     'data_body數(shù)據(jù)錯(cuò)誤 第{}行(從0開始) 需為{}個(gè)元素 當(dāng)前行{}個(gè)元素:{}'.format(count, len(self.data), len(pro), str(pro)))
   count += 1

  # step2 寫入數(shù)據(jù)
  wd = xlwt.Workbook()
  sheet = wd.add_sheet(sheet_name)

  ali_horiz = 'align: horiz center' # 水平居中
  ali_vert = 'align: vert center' # 垂直居中
  fore_colour = 'pattern: pattern solid,fore_colour pale_blue' # 設(shè)置單元格背景色為pale_blue色
  # 表頭格式(垂直+水平居中、表頭背景色)
  style_header = xlwt.easyxf('{};{};{}'.format(fore_colour, ali_horiz, ali_vert))

  # 表體格式(垂直居中)
  style_body = xlwt.easyxf('{}'.format(ali_vert))

  # 表頭
  for col in self.data:
   # 默認(rèn)列寬
   if len(col) == 1:
    sheet.write(0, self.data.index(col), str(col[0]), style_header)
   # 自定義列寬
   if len(col) == 2:
    sheet.write(0, self.data.index(col), str(col[0]), style_header)
    # 設(shè)置列寬
    sheet.col(self.data.index(col)).width = 256 * col[1] # 256為基數(shù) * n個(gè)英文字符
  # 行高(第0行)
  sheet.row(0).height_mismatch = True
  sheet.row(0).height = 20 * 20 # 20為基數(shù) * 20榜

  # 表體
  index = 1
  for pro in data_body:
   sheet.row(index).height_mismatch = True
   sheet.row(index).height = 20 * 20 # 20為基數(shù) * 20榜
   for d in self.data:
    value = pro[self.data.index(d)]
    # 若值類型是int、float 直接寫入 反之 轉(zhuǎn)成字符串寫入
    if type(value) == int or type(value) == float:
     sheet.write(index, self.data.index(d), value, style_body)
    else:
     sheet.write(index, self.data.index(d), str(value), style_body)
   index += 1
  # 凍結(jié)(列與行)
  sheet.set_panes_frozen('1')
  sheet.set_horz_split_pos(frozen_row) # 凍結(jié)前n行
  sheet.set_vert_split_pos(frozen_col) # 凍結(jié)前n列

  wd.save(out_file)

 def color_test(self):
  """
  測(cè)試顏色
  """
  body_t = []
  for color in self.color_list:
   print(color)
   body_t.append(color)
  wd = xlwt.Workbook()
  sheet = wd.add_sheet('sheet')

  index = 0
  for b in body_t:
   ali = 'align: horiz center;align: vert center' # 垂直居中 水平居中
   fore_colour = 'pattern: pattern solid,fore_colour {}'.format(
    self.color_list[index][0]) # 設(shè)置單元格背景色為pale_blue色
   style_header = xlwt.easyxf(
    '{};{}'.format(fore_colour, ali))
   sheet.write(index, 0, str(b), style_header)
   sheet.col(0).width = 256 * 150 # 256為基數(shù) * n個(gè)英文字符
   index += 1

  wd.save('顏色測(cè)試.xlsx')


# 測(cè)試顏色
# if __name__ == '__main__':
#  header_t = [
#   ['顏色']
#  ]
#  JarExcelUtil(header_t).color_test()

if __name__ == '__main__':
 header = [
  ['序號(hào)', 5],
  ['姓名', 10],
  ['性別', 10],
  ['愛好', 10],
  ['生日', 20]
 ]

 # header = [
 #  ['序號(hào)'],
 #  ['姓名'],
 #  ['性別'],
 #  ['愛好'],
 #  ['生日']
 # ]

 body = [
  [1, '張三', '男', '籃球', '1994-07-23'],
  [2, '李四', '女', '足球', '1994-04-03'],
  [3, '王五', '男', '兵乓球', '1994-09-13']
 ]

 JarExcelUtil(header_list=header).write(out_file='測(cè)試.xlsx', data_body=body)

二、效果

生成的Excel

三、常用操作

3.1、設(shè)置行高

# 行高(第0行)
sheet.row(0).height_mismatch = True
sheet.row(0).height = 20 * 20 # 20為基數(shù) * 20榜

3.2、設(shè)置列寬

# 列寬(第0列)
sheet.col(0).width = 256 * 30 # 256為基數(shù) * 30個(gè)英文字符(約)

3.3、凍結(jié)(列與行)

# 凍結(jié)(列與行)
sheet.set_panes_frozen('1')
sheet.set_horz_split_pos(2) # 凍結(jié)前2行
sheet.set_vert_split_pos(3) # 凍結(jié)前3列
  
# 凍結(jié)首行
sheet.set_panes_frozen('1')
sheet.set_horz_split_pos(1) # 凍結(jié)前1行(即首行)

3.4、設(shè)置單元格對(duì)齊方式

# 方式1
style_1 = xlwt.XFStyle()
al_1 = xlwt.Alignment()
al_1.horz = xlwt.Alignment.HORZ_CENTER # 水平居中
al_1.vert = xlwt.Alignment.VERT_CENTER # 垂直居中
style_1.alignment = al_1
sheet.write(0, 0, '第0行第0列單元格值', style_1)
  
# 方式2(推薦)
ali_horiz = 'align: horiz center' # 水平居中
ali_vert = 'align: vert center' # 垂直居中
style_2 = xlwt.easyxf('{};{}'.format(ali_horiz, ali_vert))
sheet.write(0, 0, '第0行第0列單元格值', style_2)

3.5、設(shè)置單元格背景色

# 設(shè)置單元格背景色
fore_colour = 'pattern: pattern solid,fore_colour pale_blue' # 設(shè)置單元格背景色為pale_blue色 (具體顏色值 參考上面代碼JarExcelUtil類中的color_test方法的運(yùn)行結(jié)果)
style = xlwt.easyxf('{}'.format(fore_colour))
sheet.write(0, 0, '第0行第0列單元格值', style)

以上就是Python xlrd/xlwt 創(chuàng)建excel文件及常用操作的詳細(xì)內(nèi)容,更多關(guān)于python 操作excel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中l(wèi)ist列表的一些進(jìn)階使用方法介紹

    Python中l(wèi)ist列表的一些進(jìn)階使用方法介紹

    這篇文章主要介紹了Python中l(wèi)ist列表的一些進(jìn)階使用方法介紹,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-08-08
  • python中windows鏈接linux執(zhí)行命令并獲取執(zhí)行狀態(tài)的問題小結(jié)

    python中windows鏈接linux執(zhí)行命令并獲取執(zhí)行狀態(tài)的問題小結(jié)

    這篇文章主要介紹了python中windows鏈接linux執(zhí)行命令并獲取執(zhí)行狀態(tài),由于工具是pyqt寫的所以牽扯到用python鏈接linux的問題,這里記錄一下一些碰到的問題,需要的朋友可以參考下
    2022-11-11
  • python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系

    python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系

    這篇文章主要介紹了python使用py2neo創(chuàng)建neo4j的節(jié)點(diǎn)和關(guān)系,第一步使用py2neo連接neo4j的方法然后根據(jù)dict創(chuàng)建Node,更多相關(guān)資料需要的朋友參考下面文章內(nèi)容
    2022-02-02
  • Python實(shí)現(xiàn)12306自動(dòng)搶火車票功能

    Python實(shí)現(xiàn)12306自動(dòng)搶火車票功能

    一到國慶、春節(jié)這種長假,搶火車票就非常困難?各大互聯(lián)網(wǎng)公司都推出搶票服務(wù),只要加錢給服務(wù)費(fèi)就可以增加搶到票的幾率。本文將和你一起探索搶票軟件背后的原理。
    2021-12-12
  • Python控制鼠標(biāo)鍵盤代碼實(shí)例

    Python控制鼠標(biāo)鍵盤代碼實(shí)例

    這篇文章主要介紹了Python控制鼠標(biāo)鍵盤代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • 查看Django和flask版本的方法

    查看Django和flask版本的方法

    今天小編就為大家分享一篇查看Django和flask版本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 教大家玩轉(zhuǎn)Python字符串處理的七種技巧

    教大家玩轉(zhuǎn)Python字符串處理的七種技巧

    這篇文章主要給大家介紹了關(guān)于學(xué)會(huì)Python字符串處理的七種技巧,其中包括字符串的連接和合并、字符串的切片和相乘、字符串的分割、字符串的開頭和結(jié)尾的處理、字符串的查找和匹配、字符串的替換以及字符串中去掉一些字符等操作,需要的朋友可以參考。
    2017-03-03
  • virtualenv隔離Python環(huán)境的問題解析

    virtualenv隔離Python環(huán)境的問題解析

    virtualenv為應(yīng)用提供了隔離的Python運(yùn)行環(huán)境,解決了不同應(yīng)用間多版本的沖突問題,這篇文章主要介紹了virtualenv隔離Python環(huán)境,需要的朋友可以參考下
    2022-06-06
  • python使用xlrd模塊讀取xlsx文件中的ip方法

    python使用xlrd模塊讀取xlsx文件中的ip方法

    今天小編就為大家分享一篇python使用xlrd模塊讀取xlsx文件中的ip方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Windows中安裝使用Virtualenv來創(chuàng)建獨(dú)立Python環(huán)境

    Windows中安裝使用Virtualenv來創(chuàng)建獨(dú)立Python環(huán)境

    有時(shí)我們的程序中需要調(diào)用不同版本的Python包和模塊,那么借助Virtualenv的虛擬環(huán)境就可以幫助我們隔離使用,接下來我們就來看一下在Windows中安裝使用Virtualenv來創(chuàng)建獨(dú)立Python環(huán)境的方法
    2016-05-05

最新評(píng)論