在Excel中想要写文件,大致要分为在:

原Excel文件基础上

  1. from openpyxl import load_workbook
  2. wb = load_workbook('files/p1.xlsx')
  3. sheet = wb.worksheets[0]
  4. # 找到单元格,并修改单元格的内容
  5. cell = sheet.cell(1, 1)
  6. cell.value = "新的开始"
  7. # 将excel文件保存到p2.xlsx文件中
  8. wb.save("files/p2.xlsx")

新创建Excel文件

  1. from openpyxl import workbook
  2. # 创建excel且默认会创建一个sheet(名称为Sheet)
  3. wb = workbook.Workbook()
  4. sheet = wb.worksheets[0] # 或 sheet = wb["Sheet"]
  5. # 找到单元格,并修改单元格的内容
  6. cell = sheet.cell(1, 1)
  7. cell.value = "新的开始"
  8. # 将excel文件保存到p2.xlsx文件中
  9. wb.save("files/p2.xlsx")

其它操作

  1. from openpyxl import workbook
  2. wb = workbook.Workbook() # Sheet
  3. # 1. 修改sheet名称
  4. """
  5. sheet = wb.worksheets[0]
  6. sheet.title = "数据集"
  7. wb.save("p2.xlsx")
  8. """
  9. # 2. 创建sheet并设置sheet颜色
  10. """
  11. sheet = wb.create_sheet("工作计划", 0)
  12. sheet.sheet_properties.tabColor = "1072BA"
  13. wb.save("p2.xlsx")
  14. """
  15. # 3. 默认打开的sheet
  16. """
  17. wb.active = 0
  18. wb.save("p2.xlsx")
  19. """
  20. # 4. 拷贝sheet
  21. """
  22. sheet = wb.create_sheet("工作计划")
  23. sheet.sheet_properties.tabColor = "1072BA"
  24. new_sheet = wb.copy_worksheet(wb["Sheet"])
  25. new_sheet.title = "新的计划"
  26. wb.save("p2.xlsx")
  27. """
  28. # 5.删除sheet
  29. """
  30. del wb["用户列表"]
  31. wb.save('files/p2.xlsx')
  32. """
  1. from openpyxl import load_workbook
  2. from openpyxl.styles import Alignment, Border, Side, Font, PatternFill, GradientFill
  3. wb = load_workbook('files/p1.xlsx')
  4. sheet = wb.worksheets[1]
  5. # 1. 获取某个单元格,修改值
  6. """
  7. cell = sheet.cell(1, 1)
  8. cell.value = "开始"
  9. wb.save("p2.xlsx")
  10. """
  11. # 2. 获取某个单元格,修改值
  12. """
  13. sheet["B3"] = "Alex"
  14. wb.save("p2.xlsx")
  15. """
  16. # 3. 获取某些单元格,修改值
  17. """
  18. cell_list = sheet["B2":"C3"]
  19. for row in cell_list:
  20. for cell in row:
  21. cell.value = "新的值"
  22. wb.save("p2.xlsx")
  23. """
  24. # 4. 对齐方式
  25. """
  26. cell = sheet.cell(1, 1)
  27. # horizontal,水平方向对齐方式:"general", "left", "center", "right", "fill", "justify", "centerContinuous", "distributed"
  28. # vertical,垂直方向对齐方式:"top", "center", "bottom", "justify", "distributed"
  29. # text_rotation,旋转角度。
  30. # wrap_text,是否自动换行。
  31. cell.alignment = Alignment(horizontal='center', vertical='distributed', text_rotation=45, wrap_text=True)
  32. wb.save("p2.xlsx")
  33. """
  34. # 5. 边框
  35. # side的style有如下:dashDot','dashDotDot', 'dashed','dotted','double','hair', 'medium', 'mediumDashDot', 'mediumDashDotDot','mediumDashed', 'slantDashDot', 'thick', 'thin'
  36. """
  37. cell = sheet.cell(9, 2)
  38. cell.border = Border(
  39. top=Side(style="thin", color="FFB6C1"),
  40. bottom=Side(style="dashed", color="FFB6C1"),
  41. left=Side(style="dashed", color="FFB6C1"),
  42. right=Side(style="dashed", color="9932CC"),
  43. diagonal=Side(style="thin", color="483D8B"), # 对角线
  44. diagonalUp=True, # 左下 ~ 右上
  45. diagonalDown=True # 左上 ~ 右下
  46. )
  47. wb.save("p2.xlsx")
  48. """
  49. # 6.字体
  50. """
  51. cell = sheet.cell(5, 1)
  52. cell.font = Font(name="微软雅黑", size=45, color="ff0000", underline="single")
  53. wb.save("p2.xlsx")
  54. """
  55. # 7.背景色
  56. """
  57. cell = sheet.cell(5, 3)
  58. cell.fill = PatternFill("solid", fgColor="99ccff")
  59. wb.save("p2.xlsx")
  60. """
  61. # 8.渐变背景色
  62. """
  63. cell = sheet.cell(5, 5)
  64. cell.fill = GradientFill("linear", stop=("FFFFFF", "99ccff", "000000"))
  65. wb.save("p2.xlsx")
  66. """
  67. # 9.宽高(索引从1开始)
  68. """
  69. sheet.row_dimensions[1].height = 50
  70. sheet.column_dimensions["E"].width = 100
  71. wb.save("p2.xlsx")
  72. """
  73. # 10.合并单元格
  74. """
  75. sheet.merge_cells("B2:D8")
  76. sheet.merge_cells(start_row=15, start_column=3, end_row=18, end_column=8)
  77. wb.save("p2.xlsx")
  78. """
  79. """
  80. sheet.unmerge_cells("B2:D8")
  81. wb.save("p2.xlsx")
  82. """
  83. # 11.写入公式
  84. """
  85. sheet = wb.worksheets[3]
  86. sheet["D1"] = "合计"
  87. sheet["D2"] = "=B2*C2"
  88. wb.save("p2.xlsx")
  89. """
  90. """
  91. sheet = wb.worksheets[3]
  92. sheet["D3"] = "=SUM(B3,C3)"
  93. wb.save("p2.xlsx")
  94. """
  95. # 12.删除
  96. """
  97. # idx,要删除的索引位置
  98. # amount,从索引位置开始要删除的个数(默认为1)
  99. sheet.delete_rows(idx=1, amount=20)
  100. sheet.delete_cols(idx=1, amount=3)
  101. wb.save("p2.xlsx")
  102. """
  103. # 13.插入
  104. """
  105. sheet.insert_rows(idx=5, amount=10)
  106. sheet.insert_cols(idx=3, amount=2)
  107. wb.save("p2.xlsx")
  108. """
  109. # 14.循环写内容
  110. """
  111. sheet = wb["Sheet"]
  112. cell_range = sheet['A1:C2']
  113. for row in cell_range:
  114. for cell in row:
  115. cell.value = "xx"
  116. for row in sheet.iter_rows(min_row=5, min_col=1, max_col=7, max_row=10):
  117. for cell in row:
  118. cell.value = "oo"
  119. wb.save("p2.xlsx")
  120. """
  121. # 15.移动
  122. """
  123. # 将H2:J10范围的数据,向右移动15个位置、向上移动1个位置
  124. sheet.move_range("H2:J10",rows=1, cols=15)
  125. wb.save("p2.xlsx")
  126. """
  127. """
  128. sheet = wb.worksheets[3]
  129. sheet["D1"] = "合计"
  130. sheet["D2"] = "=B2*C2"
  131. sheet["D3"] = "=SUM(B3,C3)"
  132. sheet.move_range("B1:D3",cols=10, translate=True) # 自动翻译公式
  133. wb.save("p2.xlsx")
  134. """
  135. # 16.打印区域
  136. """
  137. sheet.print_area = "A1:D200"
  138. wb.save("p2.xlsx")
  139. """
  140. # 17.打印时,每个页面的固定表头
  141. """
  142. sheet.print_title_cols = "A:D"
  143. sheet.print_title_rows = "1:3"
  144. wb.save("p2.xlsx")
  145. """