濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils

python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils

熱門(mén)標(biāo)簽:云南地圖標(biāo)注 汕頭電商外呼系統(tǒng)供應(yīng)商 北京外呼電銷(xiāo)機(jī)器人招商 400電話 申請(qǐng) 條件 crm電銷(xiāo)機(jī)器人 賓館能在百度地圖標(biāo)注嗎 電銷(xiāo)機(jī)器人 金倫通信 南京crm外呼系統(tǒng)排名 鄭州智能外呼系統(tǒng)中心

py讀寫(xiě)修改常用的三種方法

  • xlwt:用于寫(xiě)入 Excel 文件
  • xlrd:用于讀取 Excel 文件
  • xlutils:用于操作 Excel 文件的實(shí)用工具,比如復(fù)制、分割、篩選等

0、安裝模塊

pip3 install xlrd xlwt xlutils

1. 寫(xiě)入excel

git:https://github.com/python-excel/xlwt/tree/master/examples

實(shí)現(xiàn)效果

上代碼

from datetime import datetime

import xlwt

font0 = xlwt.Font()

# font0.name = 'Times New Roman' # 適用于字母或數(shù)字
font0.name = '宋體'  # 適用于中文,適配字體或者不指定字體才能體現(xiàn)出指定的顏色

# font0.colour_index = 1  # 白色
# font0.colour_index = 2  # 紅色
# font0.colour_index = 3  # 綠色
# font0.colour_index = 4  # 藍(lán)色
# font0.colour_index = 5  # 黃色
# font0.colour_index = 6  # 紫色
# font0.colour_index = 7  # 青色
# font0.colour_index = 8  # 黑色,比默認(rèn)加黑,不加粗
font0.colour_index = 4  # 藍(lán)色
font0.bold = True

style0 = xlwt.XFStyle()
style0.font = font0

# 創(chuàng)建樣式對(duì)象:日期格式
style1 = xlwt.XFStyle()
style1.num_format_str = 'YYYY-MM-DD'

# 創(chuàng)建樣式對(duì)象:字體居中對(duì)齊
style2 = xlwt.XFStyle()
al = xlwt.Alignment()
al.horz = 0x02 # 設(shè)置水平居中
al.vert = 0x01 # 設(shè)置垂直居中
style2.alignment = al

# 創(chuàng)建樣式對(duì)象,設(shè)置日期格式與字體居中對(duì)齊
style3 = xlwt.XFStyle()
style3.num_format_str = 'YYYY-MM-DD'
style3.alignment = al

# 創(chuàng)建樣式對(duì)象,設(shè)置字體居中 且 設(shè)置字體顏色
style4 = xlwt.XFStyle()
style4.alignment = al
style4.font = font0

now_time = datetime.now().strftime('%Y-%m-%d %X')
date_time = datetime.now().strftime('%Y-%m-%d')

# 創(chuàng)建表格
wb = xlwt.Workbook()

# 新建一個(gè)名為 Score Sheet 的表單頁(yè)
score_sheet = wb.add_sheet('Score Sheet')

# 新建一個(gè)名為 Record Test Sheet 的表單頁(yè)
record_test_sheet = wb.add_sheet('Record Test Sheet')

# 1、寫(xiě)入 Score Sheet 表單
# 設(shè)置 表頭, 第一個(gè)參數(shù)是行,第二個(gè)參數(shù)是列
score_sheet.write(0, 0, '時(shí)間', style2)
score_sheet.write(0, 1, '班級(jí)', style2)
score_sheet.write(0, 2, '姓名', style2)
score_sheet.write(0, 3, '語(yǔ)文', style2)
score_sheet.write(0, 4, '數(shù)學(xué)', style2)
score_sheet.write(0, 5, '英語(yǔ)', style2)
score_sheet.write(0, 6, '理綜', style2)
score_sheet.write(0, 7, '總分', style4)

# 按照位置添加數(shù)據(jù)
score_sheet.write(1, 0, datetime.now(), style3)
score_sheet.write(1, 1, '高三三班', style2)
score_sheet.write(1, 2, '桑巖', style2)
score_sheet.write(1, 3, 132, style2)
score_sheet.write(1, 4, 150, style2)
score_sheet.write(1, 5, 140, style2)
score_sheet.write(1, 6, 290, style2)
score_sheet.write(1, 7, xlwt.Formula("D2+E2+F2+G2"), style2)

score_sheet.write(2, 0, datetime.now(), style3)
score_sheet.write(2, 1, '高三三班', style2)
score_sheet.write(2, 2, '項(xiàng)天騏', style2)
score_sheet.write(2, 3, 140, style2)
score_sheet.write(2, 4, 150, style2)
score_sheet.write(2, 5, 132, style2)
score_sheet.write(2, 6, 280, style2)
score_sheet.write(2, 7, xlwt.Formula("D3+E3+F3+G3"), style2)

score_sheet.write(3, 0, datetime.now(), style3)
score_sheet.write(3, 1, '高三三班', style2)
score_sheet.write(3, 2, '向淮南', style2)
score_sheet.write(3, 3, 135, style2)
score_sheet.write(3, 4, 150, style2)
score_sheet.write(3, 5, 145, style2)
score_sheet.write(3, 6, 270, style2)
score_sheet.write(3, 7, xlwt.Formula("D4+E4+F4+G4"), style2)


# 2、寫(xiě)入 Record Test Sheet 表單
record_test_sheet.write(0, 0, '時(shí)間')
record_test_sheet.write(0, 1, '學(xué)科', style1)
record_test_sheet.write(0, 2, '成績(jī)', style1)
record_test_sheet.write(1, 0, datetime.now(), style1)
record_test_sheet.write(1, 1, '語(yǔ)文', style2)
record_test_sheet.write(1, 2, 80)
record_test_sheet.write(2, 0, datetime.now(), style3)
record_test_sheet.write(2, 1, '數(shù)學(xué)', style2)
record_test_sheet.write(2, 2, 99)
record_test_sheet.write(3, 0, now_time, style2)
record_test_sheet.write(3, 1, '英語(yǔ)', style2)
record_test_sheet.write(3, 2, 98)


# 保存表格,這里應(yīng)該是覆蓋寫(xiě),注意每次都是覆蓋所有表單內(nèi)容,建議每次生成的表單加上時(shí)間版本區(qū)分
# wb.save('example.xls')
wb.save('example-{0}.xls'.format(date_time))

2、讀 Excel

git:https://github.com/python-excel/xlrd

實(shí)現(xiàn)效果,讀取sheet 表單內(nèi)容

數(shù)值 類(lèi)型 說(shuō)明
0 empty
1 string 字符串
2 number 數(shù)字
3 date 日期
4 boole 布爾值
5 error 錯(cuò)誤

代碼

import xlrd

# 打開(kāi) xls文件
wb = xlrd.open_workbook("example-2021-03-09.xls")

# 獲取并打印 sheet 數(shù)量
print("sheet 數(shù)量:", wb.nsheets)     # sheet 數(shù)量: 2

# 獲取并打印 sheet 名稱(chēng)
print("sheet 名稱(chēng):", wb.sheet_names())  # sheet 名稱(chēng): ['Score Sheet', 'Record Test Sheet']

# 根據(jù) sheet 索引獲取內(nèi)容
sh1 = wb.sheet_by_index(0)
# 或者
# 也可根據(jù) sheet 名稱(chēng)獲取內(nèi)容
# sh = wb.sheet_by_name('Score Sheet')

# 獲取并打印該 sheet 行數(shù)和列數(shù)
print(u"sheet: %s表單 共 %d 行 %d 列" % (sh1.name, sh1.nrows, sh1.ncols))   # sheet: Score Sheet表單 共 4 行 8 列

# 獲取并打印某個(gè)單元格的值
print("第一行第二列的值為:", sh1.cell_value(0, 1))    # 第一行第二列的值為: 班級(jí)

# 獲取整行或整列的值
row_info = sh1.row_values(0)  # 獲取第一行內(nèi)容
col_info = sh1.col_values(1)  # 獲取第二列內(nèi)容

# 打印獲取的行列值
print("第一行的值為:", row_info)   # 第一行的值為: ['時(shí)間', '班級(jí)', '姓名', '語(yǔ)文', '數(shù)學(xué)', '英語(yǔ)', '理綜', '總分']
print("第二列的值為:", col_info)   # 第二列的值為: ['班級(jí)', '高三三班', '高三三班', '高三三班']

# 獲取單元格內(nèi)容的數(shù)據(jù)類(lèi)型,注意這里的值 另有含義
print("第二行第一列的【值類(lèi)型】為:", sh1.cell(1, 0).ctype)   # 第二行第一列的【值類(lèi)型】為: 3

# 遍歷所有表單內(nèi)容
for sh in wb.sheets():
  for r in range(sh.nrows):

    # 輸出指定行內(nèi)容,這里包含原有類(lèi)型指定,不能直接獲取到指定列的值
    row_val_list = sh.row(r)
    print(row_val_list)
    # [text:'時(shí)間', text:'班級(jí)', text:'姓名', text:'語(yǔ)文', text:'數(shù)學(xué)', text:'英語(yǔ)', text:'理綜', text:'總分']

    # 遍歷行內(nèi),輸出當(dāng)前行內(nèi)的所有列值
    col_val_list = [col_val.value for col_val in row_val_list]
    print(col_val_list)

3、修改 Excel

修改 Excel 是通過(guò) xlutils 庫(kù)的 copy 方法將原來(lái)的 Excel 整個(gè)復(fù)制一份,然后再做修改操作,最后再保存

修改前

修改后

上代碼

import xlrd
from xlutils.copy import copy


# 打開(kāi) excel 文件, 帶格式復(fù)制
read_book = xlrd.open_workbook("example-2021-03-09.xls", formatting_info=True)

# 復(fù)制一份
wb = copy(read_book)

# 選取第一個(gè)表單
sh1 = wb.get_sheet(0)

# 在第五行新增寫(xiě)入數(shù)據(jù)
sh1.write(4, 0, '2020-12-16')
sh1.write(4, 1, '高三三班')
sh1.write(4, 2, '小魚(yú)仙倌兒')
sh1.write(4, 3, 150)
sh1.write(4, 4, 150)
sh1.write(4, 5, 150)
sh1.write(4, 6, 300)

# 選取第二個(gè)表單
sh2 = wb.get_sheet(1)

# 替換總成績(jī)數(shù)據(jù)
sh2.write(1, 2, 100)

# 保存
wb.save('example-2021-03-09.xls')

注意,復(fù)制 xls這里有格式問(wèn)題

似乎沒(méi)有任何簡(jiǎn)單的方法可以保留單元格的格式;它總是被吹走并設(shè)置為空白。

https://www.coder.work/article/80896

https://zhuanlan.zhihu.com/p/128674458

附錄

參考:http://www.ityouknow.com/python/2019/12/29/python-excel-103.html

官網(wǎng):http://www.python-excel.org/

到此這篇關(guān)于python讀寫(xiě)修改Excel之xlrdxlwtxlutils的文章就介紹到這了,更多相關(guān)python讀寫(xiě)修改Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python 使用xlwt模塊將多行多列數(shù)據(jù)循環(huán)寫(xiě)入excel文檔的操作
  • Python中用xlwt制作表格實(shí)例講解
  • Python xlrd/xlwt 創(chuàng)建excel文件及常用操作
  • python實(shí)現(xiàn)xlwt xlrd 指定條件給excel行添加顏色
  • Python xlwt模塊使用代碼實(shí)例
  • Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù)
  • python xlwt如何設(shè)置單元格的自定義背景顏色
  • Python操作excel的方法總結(jié)(xlrd、xlwt、openpyxl)
  • python 數(shù)據(jù)生成excel導(dǎo)出(xlwt,wlsxwrite)代碼實(shí)例
  • python中使用 xlwt 操作excel的常見(jiàn)方法與問(wèn)題
  • python xlwt模塊的使用解析

標(biāo)簽:西寧 梅州 錫林郭勒盟 懷化 石家莊 昆明 浙江 文山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils》,本文關(guān)鍵詞  python,讀寫(xiě),修改,Excel,之,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于python讀寫(xiě)修改Excel之xlrd&xlwt&xlutils的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    商水县| 鹤峰县| 乐清市| 石门县| 恩平市| 上思县| 木兰县| 龙江县| 璧山县| 团风县| 庆阳市| 井冈山市| 兴义市| 华安县| 天峨县| 松原市| 汉川市| 洛川县| 静乐县| 舟山市| 修文县| 怀仁县| 唐河县| 得荣县| 来宾市| 邵东县| 肥西县| 崇礼县| 中卫市| 广水市| 屏南县| 油尖旺区| 拉萨市| 湖南省| 崇文区| 江津市| 凤城市| 南充市| 河间市| 浦东新区| 松溪县|