ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【Python实例学习】用Python的xlsxwriter模块,进行Excel表格插入图标的操作

2020-01-24 14:38:39  阅读:328  来源: 互联网

标签:sheet format Python Excel xlsxwriter excel column add test


# _*_ coding: utf-8 _*_
# 从引入xlsxwriter模块。必须提前按照该模块
import xlsxwriter

# 使用xlsxwriter模块的Workbook方法,新建20200121.xlsx,并将该表命名为excel_test
# 文件夹路径需要用左斜杠/。如果不写文件夹路径,就会存储到编译器默认路径。
excel_test = xlsxwriter.Workbook("D:/test20200121.xlsx")

# 为excel_test表格使用add_worksheet方法增加sheet标签。因为有中文,所以前面加了u
sheet_one = excel_test.add_worksheet("1month")
sheet_two = excel_test.add_worksheet("2month")
sheet_three = excel_test.add_worksheet("3month")

# 添加样式,使用字典格式:bold加粗、border边框宽度、align水平对齐方式、valign垂直对齐方式、fg_color背景色、color字体颜色、text_wrap自动换行
format1 = excel_test.add_format({"bold": True})
format2 = excel_test.add_format({"border": 1})
format3 = excel_test.add_format({"align":"center"})
format4 = excel_test.add_format({"valign":"vcenter"})
format5 = excel_test.add_format({"fg_color":"C4C4C4"})
format6 = excel_test.add_format({"color":"FF0000"})
format7 = excel_test.add_format({"text_wrap":True})
format8 = excel_test.add_format({"bold": True,"align":"center"})
format9 = excel_test.add_format(
    {"bold": True,
    "border": 1,
    "align":"center",
    "valign":"vcenter",
    "fg_color":"C4C4C4",
    "color":"FF0000",
    "text_wrap":True}
)

# 修改列宽
sheet_one.set_column("B:B", 25)
sheet_one.set_column("C:C", 15)
sheet_two.set_column("A:C", 10)

# 设置表头,自建数据,使用列表格式
headings1 = ['id', 'programe language', 'ratings(%)']
data1 = [
    [1, 2, 3, 4, 5, 6, 7, 8],
    ["Java", "C", "Python", "C++", "C#", "VB.NET", "JavaScript", "PHP"],
    [16.896, 15.773, 9.704, 5.574, 5.349, 5.287, 2.451, 2.405]
]

headings2 = [u'编号', u'排行', u'姓名']
data2 = [
    [1, 2, 3, 4, 5, 6, 7],
    [u'李大', u'陶二', u'恭三', u'麦四', u'柳五', u'钱六', u'商七'],
    [u'李沉舟', u'陶百窗', u'恭文羽', u'麦当豪', u'柳随风', u'钱山谷', u'商天良']
]

# 写入数据。sheet_one使用第一套数据,sheet_two使用第二套数据。
sheet_one.write_row("A1", headings1, format8)
sheet_one.write_column("A2", data1[0], format3)
sheet_one.write_column("B2", data1[1], format3)
sheet_one.write_column("C2", data1[2], format3)

sheet_two.write_row("A1", headings2, format9)
sheet_two.write_column("A2", data2[0], format3)
sheet_two.write_column("B2", data2[1], format3)
sheet_two.write_column("C2", data2[2], format3)

# 比较简单的插入数据和图片
sheet_three.write("B2", u"李四")
# 写入图片。# 文件夹路径需要用左斜杠/。如果不写文件夹路径,就会选择编译器默认路径的对应图片。
sheet_three.insert_image("C5", "D:/3.jpg")

# 插入图表
chart_col = excel_test.add_chart({"type":"line"})
chart_col.add_series(
    {
        "name": "=1month!$B$1",
        "categories": "=1month!$B$2:$B$9",
        "values": "=1month!$C$2:$C$9",
        "line": {"color":"red"}
    }
)

# 设置标题、X轴和Y轴的文字
chart_col.set_title({"name":u"2019年软件语言市场占有率"})
chart_col.set_x_axis({"name":u"软件语言名称"})
chart_col.set_y_axis({"name":u"占有率"})

chart_col.set_style(1)

# 选择图标的插入位置
sheet_one.insert_chart("A10",chart_col,{"x_offset":25,"y_offset":10})




# 关闭并保存Excel,必须有这段代码,否则上面所有代码将不会被保存。
excel_test.close()

 

王怕怕升职记 发布了54 篇原创文章 · 获赞 7 · 访问量 1万+ 私信 关注

标签:sheet,format,Python,Excel,xlsxwriter,excel,column,add,test
来源: https://blog.csdn.net/woshiyigerenlaide/article/details/104080313

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有