自己一个一个试出来,并写上解释。

还不熟练,待多写代码多练习。

#!/usr/bin/python
# -*- coding:utf-8 -*- import os
import xlwt
import xlrd excel_Path = (r"D:\python\The company plug-in\10.13.xlsx") data = xlrd.open_workbook(excel_Path) table = data.sheets()[0]
table = data.sheet_by_index(0)
# table = data.sheet_by_name(u"03") a = table.row_values(2) #整行数据
a = table.col_values(4) #整列数据
# for i in a:
# print i.encode("gb2312") #表格读取中文是要加这个进行转码 # nrows = table.nrows
# ncols = table.nclos
def set_style(name, height, bold=False):
style = xlwt.XFStyle() #初始化样式
font = xlwt.Font() #为样式创建字体
font.name = name #"Times New Roman"
font.bold = bold
font.color_index = 4
font.height = height style.font = font
return style def write_excel():
f = xlwt.Workbook() #创建工作薄
"""
创建工作薄
"""
sheet1 = f.add_sheet(u"sheet1", cell_overwrite_ok=True) #创建sheet
row0 = [u"业务", u"状态", u"北京", u"上海", u"广州", u"深圳",u"状态小计", u"合计"] #列 输入内容列表
column0 = [u"机票", u"船票", u"火车票", u"汽车票", u"其它"]
status = [u"预定", u"出票", u"退票", u"业务小计"]
for i in range(0, len(row0)):
sheet1.write(0, i, row0[i], set_style("Times New Roman", 300, True))#1:竖着坐标 2:横着坐标 3:写入内容 4:设置字体和字体大小
# sheet1.write_merge(1, 1, 6, 6)#5
# sheet1.write_merge(1, 3, 6, 6)#6 # ()括号里的数值 分别为 1: 2:竖着数几行 3:
# sheet1.write_merge(1, 5, 5, 6)# 7 # ()括号里的数值 分别为 1: 2:竖着数几行 3和4:从那列到那列进行合并
# sheet1.write_merge(0, 5, 5, 6)# 8 # ()括号里的数值 分别为 1:竖着从第几行开始 2:竖着数几行格式化 3和4:从那列到那列进行合并 i, j = 1, 0
while i < 4 * len(status) and j < len(column0):
# print "yes"
sheet1.write_merge(i, i + 3, 0, 0, column0[j], set_style("Times New Roman", 250, True))
#分别为 1:竖着从第几行开始 2:竖着数几行格式化 3和4:从那列到那列进行合并 5:写入内容 6:设置字体和大小
sheet1.write_merge(i, i + 3, 7, 7,) # 最后一列"合计"
i += 4
j += 1
i = 0
while i < len(status) * len(status):
for a in status:
# print status[i]
sheet1.write(i + 1, 1, a, set_style("Times New Roman", 200, True))
i += 1
print i
sheet1.write_merge(23, 23, 0, 1, row0[7], set_style("Times New Roman", 300, True)) f.save("test23.xlsx") # #生成第一行
#
# for i in range(0, len(row0)):
# # print (0, i, row0[i], set_style("Times New Roman", 220, True))
# sheet1.write(0, i, row0[i], set_style("Times New Roman", 220, True))
#
# #生成第一列和最后一列(合并4行)
# i, j = 1, 0
# while i < 4 * len(column0) and j < len(column0):
# sheet1.write_merge(i, i+3, 0, 0, column0[j], set_style("Arial", 220, True)) #第一列
# sheet1.write_merge(i, i + 3, 7, 7,) #最后一列"合计"
# i += 4
# j += 1
# sheet1.write_merge(21, 21, 0, 1, u"合计", set_style("Times New Roman", 220, True))
#
# #生成第二列
# i = 0
# while i < 4 * len(column0):
# for j in range(0, len(status)):
# sheet1.write(j + i + 1, 1, status[j])
# i += 4
# f.save("demo1.xlsx") #保存文件
#
if __name__ == "__main__": # 测试,判断是不是被人导入,被人当模块使用的时候 就等于文件名
#generate_workbook()
#read_rxcel()
write_excel() # for i in os.listdir(excel_Path):
# if "20170602_A" in i:
# j = os.path.join(excel_Path, i)
# # k = xlrd.open_workbook(j)
# print j

最新文章

  1. DEDECMS之八 漏洞错误和疑难杂症
  2. AX Query
  3. windows下配置Apache2.4一些改变
  4. wcf service library
  5. EBS与FMW集成工作流管理器的检查
  6. sql server里一些常用的查询
  7. .NET Core 2.0 开源Office组件 NPOI
  8. JavaScript笔记之第六天
  9. AutoMapper IIS回收引发的 未将对象引用设置到对象实例
  10. vue cli创建的项目 当你后期使用了ES6语法,如何解决浏览器兼容问题
  11. Go pprof性能监控
  12. 基于DBUtils实现数据库连接池
  13. 老毛桃UEFI版u盘启动盘
  14. [Linux.NET]在CentOS 7.x中编译方式安装Nginx
  15. [LeetCode] 577. Employee Bonus_Easy tag: SQL
  16. GameObject.SendMessage
  17. Ansible中playbook的变量
  18. openvpn 负载均衡方案
  19. SnapKit 约束创建过程
  20. 制作一个64M的U盘启动盘(mini linux + winpe +dos toolbox)

热门文章

  1. 重启标志log
  2. luogu 3383【模板】线性筛素数
  3. linux 条件测试 ******
  4. Timer A UP mode 中断
  5. 洛谷P2668斗地主(搜索)noip2015
  6. 【转】vue.js三种安装方式
  7. 题解报告:hdu 1564 Play a game(找规律博弈)
  8. MySQL的安装和启动
  9. 【LeetCode】-- 260. Single Number III
  10. [转]Linux定时任务Crontab详解