Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1790586
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2017-05-28 16:35:45

1.先转两篇文章:
python读写Excel和读写Xml. https://linsir.org/post/read-and-write-excel-and-xml-files-with-python
用python读写Excel文件 http://www.gocalf.com/blog/python-read-write-excel.html

2.xlsxwriter的官方文档在这里:
https://xlsxwriter.readthedocs.io/

3.贴下我照抄别人的代码:

点击(此处)折叠或打开

  1. import xlsxwriter

  2. workbook = xlsxwriter.Workbook("add_sheet.xlsx")
  3. worksheet = workbook.add_worksheet(name="New Sheet 2")
  4. expenses = (
  5.     ["Rent", 1000],
  6.     ["Gas", 100],
  7.     ["Food", 300],
  8.     ["Gym", 50],
  9.     )

  10. row = 0
  11. col = 0
  12. for item,cost in (expenses):
  13.     worksheet.write(row, col, item)
  14.     worksheet.write(row, col+1, cost)
  15.     row += 1
  16. worksheet.write(row, 0, 'Total')
  17. worksheet.write(row, 1, '=SUM(B1:B4)')

  18. format1 = workbook.add_format({"bg_color":"blue",
  19.     "font_color": "red"})

  20. worksheet.conditional_format("B1:KB5",
  21.     {"type": "cell",
  22.      "criteria": ">=",
  23.      "value": 150,
  24.      "format": format1})


  25. workbook.close()

效果如下: 我发现我的SUM公式没有算结果,显示为0,不知道为什么.我用的是libreoffice 5.3 .


下面是个有chart的例子,同样的公式仍然是没有计算出来结果.

点击(此处)折叠或打开

  1. import xlsxwriter

  2. workbook = xlsxwriter.Workbook("chart_column.xlsx")

  3. worksheet = workbook.add_worksheet()
  4. chart = workbook.add_chart({"type" : "column"})

  5. data = [
  6.     [ "Year", "2013", "2014", "2015" ],
  7.     [ "Revenue", 100, 120, 125 ],
  8.     [ "COGS", 80, 90, 70 ],
  9.     [ "Profit", 20, 30, 55 ],
  10.     ]

  11. worksheet.write_row("A1", data[0])
  12. worksheet.write_row("A2", data[1])
  13. worksheet.write_row("A3", data[2])
  14. worksheet.write_row("A4", data[3])

  15. chart.add_series({"values" :"=Sheet1!$B$2:$B$4","name":"2013"})
  16. chart.add_series({"values" :"=Sheet1!$C$2:$C$4","name":"2014"})
  17. chart.add_series({"values" :"=Sheet1!$D$2:$D$4","name":"2015"})

  18. worksheet.insert_chart("G1", chart)

  19. worksheet.write(5, 0, "% Gain")
  20. worksheet.write(5, 1, "=(B4/B2)*100")
  21. worksheet.write(5, 2, "=(C4/C2)*100")
  22. worksheet.write(5, 3, "=(D4/D2)*100")

  23. workbook.close()

结果如下:

阅读(1237) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~