Python操作excel

287 阅读2分钟
  1. import xlrd
  2. import xlwt
  3. from datetime import date, datetime


  4. def read_excel():
  5. # 1 打开文件
  6. workbook = xlrd.open_workbook('表格1.xlsx')

  7. # 2 获取sheet的名字
  8. sheet2_name = workbook.sheet_names()[0]
  9. print("获取sheet的名字", sheet2_name)

  10. # 3 获得sheet的内容
  11. # 根据sheet索引或者名称获取sheet内容
  12. sheet2 = workbook.sheet_by_index(0) # sheet索引从0开始
  13. # sheet2 = workbook.sheet_by_name('sheet2')

  14. # 4 sheet的名称,行数,列数
  15. print(sheet2.name, sheet2.nrows, sheet2.ncols)

  16. # 5 获取整行和整列的值(数组)
  17. # rows = sheet2.row_values(2) # 获取第二行内容 list
  18. # cols = sheet2.col_values(4) # 获取第四列内容 list
  19. # print(rows, cols)

  20. # 6 获取单元格内容
  21. print(sheet2.cell(1, 0).value)
  22. print(sheet2.cell_value(1, 0))
  23. print(sheet2.row(1)[0].value)

  24. # 7 获取单元格内容的数据类型
  25. print(sheet2.cell(1, 0).ctype)


  26. # 其它
  27. # sheet1 = wb.sheet_by_index(0) # 通过索引获取表格
  28. # sheet2 = wb.sheet_by_name('年级') # 通过名字获取表格
  29. # print(sheet1, sheet2)
  30. # print(sheet1.name, sheet1.nrows, sheet1.ncols)

  31. def write_excel():
  32. """书写数据是以sheet为单位的,所以首先要创建新的sheet"""

  33. # 0 创建对象
  34. f = xlwt.Workbook()

  35. # 1 创建新的sheet了 # 参数1:名字,参数2:是否可以覆盖写入
  36. sheet1 = f.add_sheet('2018年账目', cell_overwrite_ok=True)

  37. # 2 写入函数 sheet1.write(x, y, str)
  38. row0 = ['月', '日', '凭证编号 ', '摘要', '对应科目', '借方金额', ' 贷方金额', '余额']
  39. colum0 = [12, 12, 12]
  40. colum1 = [2, 3, 4]
  41. colum2 = [20181202, 20181203, 20181204]
  42. colum3 = ['A原料公司进货款100万', 'B经销公司货款30万', 'c经销公司货款30万']
  43. colum4 = ['成本', '销售', '销售']
  44. # 写第一行
  45. for i in range(0, len(row0)):
  46. sheet1.write(0, i, row0[i], set_style('Times New Roman', 220, True))

  47. # 写第一列
  48. for i in range(0, len(colum0)):
  49. sheet1.write(i + 1, 0, colum0[i], set_style('Times New Roman', 220, True))

  50. # 写第一列
  51. for i in range(0, len(colum1)):
  52. sheet1.write(i + 1, 1, colum1[i], set_style('Times New Roman', 220, True))

  53. # 写第一列
  54. for i in range(0, len(colum2)):
  55. sheet1.write(i + 1, 2, colum2[i], set_style('Times New Roman', 220, True))

  56. # 写第一列
  57. for i in range(0, len(colum3)):
  58. sheet1.write(i + 1, 3, colum3[i], set_style('Times New Roman', 220, True))

  59. # 写第一列
  60. for i in range(0, len(colum4)):
  61. sheet1.write(i + 1, 4, colum4[i], set_style('Times New Roman', 220, True))
  62. # 3 合并单元格写入
  63. # sheet1.write_merge(7, 8, 2, 4, 'ceshi')
  64. # 合并第8-9行,第3-5列,写入ceshi
  65. # sheet1.write_merge('对应科目', '原料', '销售')
  66. # 4 添加超链接
  67. # n = "HYPERLINK"
  68. # sheet1.write_merge(9, 9, 2, 8, xlwt.Formula(n + '("http://www.baidu.com")'), set_style('Arial', 300, True))
  69. f.save("表格1.xlsx")
  70. print("done")


  71. def set_style(name, height, bold=False):
  72. style = xlwt.XFStyle() # 初始化样式
  73. font = xlwt.Font() # 为样式创建字体
  74. font.name = name # 'Times New Roman'
  75. font.bold = bold
  76. font.color_index = 4
  77. font.height = height
  78. style.font = font
  79. return style


  80. if __name__ == '__main__':
  81. read_excel()
  82. write_excel()
复制代码

更多python学习资料可关注:gzitcast