当前位置:   article > 正文

python读取所有sheet内容到另一个文件中_python 提取excel各sheet固定的内容另存

python 提取excel各sheet固定的内容另存

 实现效果:

将原excel中的步骤、预期效果列按回车拆成多行数据,其余字段值填充其他数据

实现结果:

  1. # This is a sample Python script.
  2. # Press Shift+F10 to execute it or replace it with your code.
  3. # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
  4. # def print_hi(name):
  5. # # Use a breakpoint in the code line below to debug your script.
  6. # print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
  7. #
  8. #
  9. # # Press the green button in the gutter to run the script.
  10. # if __name__ == '__main__':
  11. # print_hi('PyCharm')
  12. #
  13. # # See PyCharm help at https://www.jetbrains.com/help/pycharm/
  14. import openpyxl, re
  15. def SplitLine():
  16. book = openpyxl.load_workbook(file_path + "\\" + raw_excel) # 读取原excel
  17. wb = book.active
  18. book_2 = openpyxl.load_workbook(file_path + "\\" + "result.xlsx") # 读取新建的result
  19. wb_2 = book_2.active
  20. name = ["TCID", "Test Summary", "Test Priority", "Step", "Data", "Expected Result", "Test Repository Path",
  21. "Pre-condition", "TestCaseType", "Automation", "LinkType", "LinkIssue"]
  22. # name = ["所属模块(*)", "用例标题(*)", "前置条件", "步骤", "预期结果", "优先级"] # result表格的首行内容
  23. for a in range(1, len(name) + 1):
  24. wb_2.cell(row=1, column=a, value=name[a - 1]) # 写入result的首行
  25. j = 2 #新建excel的行数
  26. k = 0 #回车数量
  27. b = 2
  28. #遍历所有的sheet
  29. for sheet_name in book.sheetnames:
  30. print(book.sheetnames)
  31. sheet = book[sheet_name]
  32. a = sheet.cell(2,2).value
  33. print(a)
  34. print(sheet)
  35. print(sheet.max_row)
  36. with open(file_path + "\\" + "result.xlsx",encoding='utf-8') as f:
  37. for i in range(2, sheet.max_row+1):
  38. # 读取原excel的每行内容
  39. Test_Repository_Path = sheet.cell(i, 1).value
  40. try:
  41. Test_Repository_Path = "/对外系统用例/"+sheet_name+"/"+Test_Repository_Path
  42. except:
  43. print("继续执行")
  44. # print(wb.cell(1, 1).value)
  45. print(Test_Repository_Path)
  46. Test_Summary = sheet.cell(i, 2).value
  47. Pre_condition = sheet.cell(i, 3).value
  48. Data = sheet.cell(i, 4).value
  49. print(Data)
  50. Expected_Result = sheet.cell(i, 5).value
  51. # 将内容写入result中
  52. wb_2.cell(row=j, column=7, value=Test_Repository_Path)
  53. wb_2.cell(row=j, column=2, value=Test_Summary)
  54. wb_2.cell(row=j, column=3, value="Medium")
  55. wb_2.cell(row=j, column=8, value=Pre_condition)
  56. wb_2.cell(row=j, column=5, value=Data)
  57. wb_2.cell(row=j, column=6, value=Expected_Result)
  58. wb_2.cell(row=j, column=9, value="功能用例")
  59. wb_2.cell(row=j, column=10, value="否")
  60. wb_2.cell(row=j, column=1, value=b-1)
  61. wb_2.cell(row=j, column=4, value=1)
  62. try:
  63. # 对Data中的内容进行识别
  64. if '\n' in Data:
  65. k = Data.count("\n")
  66. k = k + 1 # 数据量比“;”的个数多一个
  67. Data_2 = []
  68. Expected_Result_2 = []
  69. #循环所有的回车数量,分隔到每一行中
  70. for p in range(0, k):
  71. Data_2.append(re.split(r"[\n]\s*", Data.split("\n")[p]))
  72. Expected_Result_2.append(re.split(r"[\n]\s*", Expected_Result.split("\n")[p]))
  73. # print(Data_2)
  74. # print(Data_2[p][0])
  75. # # print(Data_2[0][p])
  76. wb_2.cell(row=j, column=5, value=str(Data_2[p][0]))
  77. wb_2.cell(row=j, column=6, value=str(Expected_Result_2[p][0]))
  78. wb_2.cell(row=j, column=4, value=p+1)
  79. wb_2.cell(row=j, column=1, value=b - 1)
  80. # print(wb_2.cell(row=j, column=4).value)
  81. j = j + 1
  82. else:
  83. wb_2.cell(row=j, column=5, value=Data)
  84. wb_2.cell(row=j, column=6, value=Expected_Result)
  85. wb_2.cell(row=j, column=1, value=b - 1)
  86. j = j + 1
  87. except:
  88. print("继续执行")
  89. b = b + 1
  90. book_2.save(file_path + "\\" + "result.xlsx") # 保存excel
  91. if __name__ == "__main__":
  92. file_path =r'C:\Users\12133\Desktop'
  93. raw_excel ='对外系统用例.xlsx'
  94. # file_path = input("file_path:")
  95. # raw_excel = input("excel_name:")
  96. SplitLine()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/768650
推荐阅读
相关标签
  

闽ICP备14008679号