荐 Python自动化办公——xlrd、xlwt读写Excel

2022-08-04,,,,

一、xlrd、xlwt读写Excel

1、读操作

import xlrd

# 1、打开工作本workbook
xlsx = xlrd.open_workbook(r'.\7月下旬入库表.xlsx')

# 2、打开需要操作的表sheet
table = xlsx.sheet_by_index(0)
# table = xlsx.sheet_by_name('7月下旬入库表')

# 3、读取指定单元格的数据
print(table.cell_value(1,1))
print(table.cell(1,1).value)
print(table.row(1)[1].value)

2、写操作

import xlwt

# 1、新建一个工作本
new_workbook = xlwt.Workbook()

# 2、为这个工作本中添加一个工作表
worksheet = new_workbook.add_sheet('new_test')

# 3、向指定单元格写入内容
worksheet.write(0,0,'test')

# 4、保存
new_workbook.save('./test.xls')

3、带文字格式的写入操作

from xlutils.copy import copy
import xlrd
import xlwt

# 1、打开需要进行操作的工作本,并将其进行复制操作
# 注意:使用读取xls文件的时候都是使用的xlrd库,但是这个库只能操作 .xls格式,对于后来的 .xlsx的版本支持不算太好
# formatting_info 该参数默认为False,这可以节省内存;当取值为True时,会读取各种格式的信息
tem_excel = xlrd.open_workbook(r'.\日统计.xls',formatting_info=True)

# 2、选择需要操作的工作表
tem_sheet = tem_excel.sheet_by_index(0)

# 3、新建一个工作本,通过复制模板工作本的方式
new_excel = copy(tem_excel)

# 4、选择新的工作本中需要操作的工作表
new_sheet = new_excel.get_sheet(0)



# 初始化样式
style = xlwt.XFStyle()

# 初始化字体
font = xlwt.Font()
font.name = '微软雅黑'
font.bold = True
font.height = 18 * 20
style.font = font

# 初始化边框,细边框
borders = xlwt.Borders()
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
style.borders = borders

# 初始化对齐方式
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style.alignment = alignment



# 5、向已选择的工作表中写入数据
new_sheet.write(2,1,12,style)
new_sheet.write(3,1,18,style)
new_sheet.write(4,1,19,style)
new_sheet.write(5,1,15,style)





# 6、保存文件
new_excel.save('./填写.xls')





4、综合案例:

import xlrd
import xlwt
from xlutils.copy import copy

xlsx = xlrd.open_workbook(r'.\7月下旬入库表.xlsx')
table = xlsx.sheet_by_index(0)

all_data = []

# 循环读取每一行的中的数据
for n in range(1,table.nrows):
    company = table.cell(n,1).value # 销售商
    price = table.cell(n,3).value # 单价
    weight = table.cell(n,4).value # 入库量

    data = {'company':company,'weight':weight,'price':price}
    all_data.append(data)

# 以下内容可以用pandas的groupby轻易实现,这里用了一个笨方法
a_weight = []
a_total_price = []

b_weight = []
b_total_price = []

c_weight = []
c_total_price = []

d_weight = []
d_total_price = []


# 计算每个销售商的总入库量 和 总价
for i in all_data:
    if i['company'] == '张三粮配':
        a_weight.append(i['weight'])
        a_total_price.append(i['weight'] * i['price'])
    if i['company'] == '李四粮食':
        b_weight.append(i['weight'])
        b_total_price.append(i['weight'] * i['price'])
    if i['company'] == '王五小麦':
        c_weight.append(i['weight'])
        c_total_price.append(i['weight'] * i['price'])
    if i['company'] == '赵六麦子专营':
        d_weight.append(i['weight'])
        d_total_price.append(i['weight'] * i['price'])

tem_excel = xlrd.open_workbook(r'.\7月下旬统计表.xls',formatting_info=True)
tem_sheet = tem_excel.sheet_by_index(0)

new_excel = copy(tem_excel)
new_sheet = new_excel.get_sheet(0)

style = xlwt.XFStyle()

# 初始化字体
font = xlwt.Font()
font.name = '微软雅黑'
font.bold = True
font.height = 18 * 20
style.font = font

# 初始化边框,细边框
borders = xlwt.Borders()
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THIN
borders.left = xlwt.Borders.THIN
borders.right = xlwt.Borders.THIN
style.borders = borders

# 初始化对齐方式
alignment = xlwt.Alignment()
alignment.horz = xlwt.Alignment.HORZ_CENTER
alignment.vert = xlwt.Alignment.VERT_CENTER
style.alignment = alignment

new_sheet.write(2,1,len(a_weight),style)
new_sheet.write(2,2,round(sum(a_weight)),style)
new_sheet.write(2,3,round(sum(a_total_price),2),style)

new_sheet.write(3,1,len(b_weight),style)
new_sheet.write(3,2,round(sum(b_weight)),style)
new_sheet.write(3,3,round(sum(b_total_price),2),style)

new_sheet.write(4,1,len(c_weight),style)
new_sheet.write(4,2,round(sum(c_weight)),style)
new_sheet.write(4,3,round(sum(c_total_price),2),style)

new_sheet.write(5,1,len(d_weight),style)
new_sheet.write(5,2,round(sum(d_weight)),style)
new_sheet.write(5,3,round(sum(d_total_price),2),style)

new_excel.save('./7月下旬统计表.xls')

本文地址:https://blog.csdn.net/weixin_44827418/article/details/107296893

《荐 Python自动化办公——xlrd、xlwt读写Excel.doc》

下载本文的Word格式文档,以方便收藏与打印。