python写入excel文件

# coding:utf-8
import xlrd
import xlwt


# 设置表格样式
def set_style(name, height, bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style


def add_sheet(excel_obj, sheet_name):
    """
    增加sheet页
    :param excel_obj: 创建的excel文件对象
    :param sheet_name: sheet名称
    :return: new_sheet 返回新创建的sheet对象
    """
    # cell_overwrite_ok=True表示一个单元格可以被多次覆盖写入
    new_sheet = excel_obj.add_sheet(sheet_name, cell_overwrite_ok=True)
    return new_sheet


def add_sheet_row(sheet_obj, fieldnames, row_index=0):
    """
    在sheet中写入一行内容
    :param sheet_obj: sheet对象
    :param fieldnames: 要写入的内容列表,列表中每个元素写入一个单元格
    :param row_index: 要写入的行号,默认写入第0行
    :return:
    """
    for i in range(len(fieldnames)):
        # 在第0行,第i列,以set_style设置的格式,写入fieldnames[i]中的内容
        sheet_obj.write(row_index, i, fieldnames[i], set_style('Times New Roman', 220, True))


def add_sheet_col(sheet_obj, fieldnames, col_index=0, row_index=1):
    """
    在sheet的某一列中写入内容
    :param sheet_obj: sheet对象
    :param fieldnames: 要写入的内容列表,列表中每个元素写入一个单元格
    :param col_index: 要写入的列编号,默认滴入第0列
    :param row_index: 从row_index行开始写入,默认从第一行开始写,第0行是标题
    :return:
    """
    for i in range(len(fieldnames)):
        sheet_obj.write(row_index + i, col_index, fieldnames[i], set_style('Times New Roman', 220, True))


def add_sheet_cell(sheet_obj, cell, row_index, col_index):
    """
    在sheet的某个单元格中写入内容
    :param sheet_obj: sheet对象
    :param cell: 待写入的内容
    :param row_index: 单元格的行号
    :param col_index: 单元格的列号
    :return:
    """
    sheet_obj.write(row_index, col_index, cell)


def merge_cell(sheet_obj, row1_index, row2_index, col1_index, col2_index, cell):
    """
    合并单元格
    :param sheet_obj:sheet对象
    :param row1_index:起始单元格的行号
    :param row2_index:截止单元格的行号
    :param col1_index:起始单元格的列号
    :param col2_index:截止单元格的列号
    :param cell: 单元格合并后写入的内容
    :return:
    """
    sheet_obj.write_merge(row1_index, row2_index, col1_index, col2_index, label=cell)


# 写Excel
def write_excel():
    f = xlwt.Workbook()
    sheet1 = add_sheet(f, '学生')
    row0 = ["姓名", "年龄", "出生日期", "爱好"]
    colum0 = ["张三", "李四", "恋习Python", "小明", "小红", "无名"]

    # 写入一行(属性列,如姓名、年龄、性别)
    add_sheet_row(sheet1, row0)
    # 写入一列
    add_sheet_col(sheet1, colum0, 0, 1)
    # 写入一个单元格
    add_sheet_cell(sheet1, '2006/12/12', 1, 2)

    # 合并单元格
    merge_cell(sheet1, 6, 6, 1, 3, '未知')    # 合并行单元格,合并第6行的1/2/3三个单元格
    merge_cell(sheet1, 1, 2, 3, 3, '打游戏')   # 合并列单元格,合并第1/2行的第3个单元格
    merge_cell(sheet1, 4, 5, 3, 3, '打篮球')

    f.save('./data/test_write.xls')


if __name__ == '__main__':
    write_excel()

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值