Python笔记--第三方库--XLSXWRITER模块--操作

写入文本到excel

import xlsxwriter

wb = xlsxwriter.Workbook("xlsxdata.xlsx")
#创建sheet
sheet = wb.add_worksheet("xlsxsheet")
#写入
sheet.write(0, 0, "账号信息")   #写入单元格数据
sheet.merge_range(2, 0, 2, 1, "第一组")
data = (["liming","123456"],["wangfei","1234567"])
sheet.write_row(3,0,["账号名称", "密码"])

#写入数据
for i,item in enumerate(data):
    sheet.write_row(i+4, 0, item)
#关闭流
wb.close()

写入公式到excel

import xlsxwriter

wb = xlsxwriter.Workbook("xlsxdata.xlsx")
#创建sheet
sheet = wb.add_worksheet("xlsxsheet")
#写入标题和表头
sheet.merge_range(0, 0, 1, 1, "工资")
sheet.write_row(2,0,["月份", "实发薪资"])
data = (["一月", 500], ["二月", 600])
#写入数据
for i,item in enumerate(data):
    sheet.write_row(i+3, 0, item)
#使用sum函数
sheet.write(5, 0, "合计")
sheet.write(5, 1, "=sum(B4:B5)")

#关闭流
wb.close()

写入图片到excel

import xlsxwriter

wb = xlsxwriter.Workbook("xlsxdata.xlsx")
#创建sheet
sheet = wb.add_worksheet("xlsxsheet")

sheet.insert_image(0, 0, "hf.png")

#关闭流
wb.close()

写入URL到excel

import xlsxwriter

wb = xlsxwriter.Workbook("xlsxdata.xlsx")
#创建sheet
sheet = wb.add_worksheet("xlsxsheet")

sheet.write_url(0, 0, "http://www.baidu.com", string = "百度一下")

#关闭流
wb.close()

 

xlsxwriter安装:https://blog.csdn.net/ilifei/article/details/107383976

你可能感兴趣的:(python)