0


AI帮我写python脚本,效率提升N倍

当前在初创芯片公司,工作中经常需要通过脚本实现效率提升。因为公司人少,都需要自己空闲时间做一些脚本。最近使用AI做了两个尝试,明显能感觉到AI带给我的效率提升。以前是我需要自己梳理需求、写代码、调试。现在用上AI,我只需要自己提需求,然后迭代需求,测试和微调脚本即可。而且在使用AI的时候,也领会到一点和AI打交道的方法。

先介绍一下两个脚本的用途,最后会贴上可以使用的脚本代码。

脚本1:文件列表展开工具,命令行方式运行。因为芯片编译的filelist经常是嵌套的,且带环境变量的,但是有些工具(例如FPGA编译工具)需要读取打平的filelist。

脚本2:excel反标工具,带图形界面。在芯片开发末期,要做一些质量活动——就是确认下游是否覆盖了上游的需求,例如验证是否覆盖所有设计的功能点。经常需要在设计的功能点(excel文件A)上标注对应验证的测试点(excel文件B)的编号,这个工作就是需要从文件B中复制测试点编号的单元格内容,到文件A中。过程中有大量的ctrl+c和ctrl+v的操作,每次做下来,左手累的不行。希望有个脚本能实现ctrl+c和ctrl+v的动作。

1、文件列表展开脚本

要处理的文件层级如下:

顶层的filelist如下:

子级filelist内容如下:

AI提示词V1.0:

帮我写一个python脚本。工作环境为linux。filest文件中每一行是一个文件路径,把其中的环境变量展开,然后输出为一个新的文件。filelist中还会嵌套filelist,也需要展开。filelist的行前缀-f。删除文件路径两侧的空格。

生成的代码V1.0:

  1. import os
  2. def expand_env_variables(path):
  3. """展开环境变量"""
  4. return os.path.expandvars(path)
  5. def process_file_list(file_list_path, output_path):
  6. """处理文件列表,展开环境变量,并输出到新文件"""
  7. expanded_paths = []
  8. with open(file_list_path, 'r') as file:
  9. for line in file:
  10. # 删除行首尾的空格
  11. line = line.strip()
  12. # 检查是否是文件列表的行
  13. if line.startswith('-f'):
  14. # 递归处理嵌套的filelist
  15. nested_file_list_path = expand_env_variables(line[2:])
  16. nested_paths = process_file_list(nested_file_list_path, output_path)
  17. expanded_paths.extend(nested_paths)
  18. else:
  19. # 展开环境变量并添加到列表
  20. expanded_path = expand_env_variables(line)
  21. expanded_paths.append(expanded_path)
  22. # 将展开后的路径写入到输出文件
  23. with open(output_path, 'w') as output_file:
  24. for path in expanded_paths:
  25. output_file.write(path + '\n')
  26. # 脚本的入口点
  27. if __name__ == "__main__":
  28. file_list_path = 'filelist' # 假设你的文件名为filelist
  29. output_file_path = 'expanded_filelist' # 输出文件的名称
  30. process_file_list(file_list_path, output_file_path)

这个脚本有3个问题:

1、就是process_file_list每次调用都会打开文件。但是递归调用的时候,写入有些问题

2、嵌套的filelist路径还会包含空格,会出错。看来去掉空格这个命令,AI理解的和我不同。

3、只处理嵌套的filelist,而且递归调用处理二级fielist时,会返回空(因为只处理前缀-f的行),导致运行出错。

AI提示词V2.0

帮我写一个python脚本。工作环境为linux。filest文件中每一行是一个文件路径,把其中的环境变量展开,然后输出为一个新的文件。filelist中还会嵌套filelist,也需要展开。filelist中嵌套的filelist的行前缀-f,其他的文件没有前缀。删除文件路径两侧的空格。文件路径提取后先保存在一个列表中,读取完成后,再一次性输出到文件。递归执行时,返回列表,不要直接写入文件。先把路径两侧可能存在的空格去掉,再展开环境变量。

生成的代码V2.0

  1. import os
  2. def expand_path(path):
  3. # 去除路径两侧的空格并展开环境变量
  4. return os.path.expandvars(os.path.expanduser(path.strip()))
  5. def process_filelist(filelist_path, expanded_paths):
  6. # 读取filelist文件
  7. with open(filelist_path, 'r') as file:
  8. for line in file:
  9. original_line = line
  10. # 去除行首尾的空格
  11. stripped_line = line.strip()
  12. # 检查是否是嵌套的filelist文件
  13. if stripped_line.startswith('-f'):
  14. # 去除'-f'前缀并处理嵌套的filelist
  15. nested_filelist_path = expand_path(stripped_line[2:])
  16. if os.path.isfile(nested_filelist_path):
  17. # 递归处理嵌套的filelist文件
  18. process_filelist(nested_filelist_path, expanded_paths)
  19. else:
  20. # 展开环境变量
  21. expanded_path = expand_path(stripped_line)
  22. # 确保路径是文件并且存在,避免重复添加
  23. if os.path.isfile(expanded_path) and expanded_path not in expanded_paths:
  24. expanded_paths.append(expanded_path)
  25. return expanded_paths
  26. def main():
  27. # filelist文件的路径
  28. filelist_path = 'filelist'
  29. # 用于存储所有文件路径的列表
  30. expanded_paths = []
  31. # 处理filelist文件并获取所有路径
  32. expanded_paths = process_filelist(filelist_path, expanded_paths)
  33. # 将路径列表写入到新文件
  34. with open('expanded_file_paths.txt', 'w') as output_file:
  35. for path in expanded_paths:
  36. output_file.write(path + '\n')
  37. print(f"Expanded paths have been written to 'expanded_file_paths.txt'")
  38. if __name__ == "__main__":
  39. main()

运行结果

2、excel反标工具

由于生成的代码比较长,我就每个都贴了。只是贴一下提示词,以及生成的脚本的缺点。

提示词V1.0

帮我写一个python脚本。使用pywin32和pyside6。需求如下:1、支持从当前已经打开的excel文件中选择excel文件;2、支持提取两个excel文件中选中的单元格的内容;3、提供界面,界面有2个下拉菜单,下拉菜单中为当前打开的excel文件名,支持从其中选择excel文件;还需要一个刷新按钮,更新下拉菜单; 4、支持界面置顶显示,也可以取消置顶显示,置顶选择按钮按下表示置顶,弹起表示取消置顶;5、支持通过一个按键(颜色为蓝色),关联两个单元格的内容——关联方式为,将其中一个文件单元格拷贝另一个文件的同一行的某列,列的位置,可以通过界面下拉菜单选择。6、界面有一个刷新按钮,用于更新excel文件列表。7、界面还支持一个按钮来表示复制的方向,按钮弹起时,表示从第一个文件复制到第二个文件的某个单元格,按键按下时表示从第二个文件复制到第一个文件的某个单元格,此单元格与excel中已选中的单元格同行,列的位置由界面选择的列位置来决定。

生成的GUI界面:

问题:可以生成图形界面每个按钮有对应的调用函数,但是调用函数里面只有一些打印提示,没有对应的功能(虽然我在提示词中对每个按钮的功能有描述,但是AI看起来只理解到对界面的诉求,没有理解按钮背后要做的动作。

提示词V2.0

提示词改进点:增加功能需求概述,放在提示词最前面;对界面显示区域进行划分。

帮我写一个python脚本。使用pywin32和pyside6。脚本支持从当前已经打开的excel文件中选择excel文件,支持复制其中一个excel文件中选中的单元格的内容,粘贴到另外一个excel选中的单元格中;脚本还支持界面。

脚本的界面需要分3个区域。

区域1包含如下控件:静态文本,内容为”Excel文件1”,按照最小长度显示;下拉菜单A,显示当前已经打开的excel的文件名,下拉菜单通过“更新”按钮刷新下拉选项。

区域2包含如下控件:静态文本,内容为”Excel文件2”,按照最小长度显示;下拉菜单A,显示当前已经打开的excel的文件名,下拉菜单通过“更新”按钮刷新下拉选项。

区域3包含控件:刷新按钮,按钮按下时,重新检测已经打开的excel文件,更新区域1和2中的下拉菜单,刷新前保留之前选中的文件名,更新下来菜单后,如果之前下拉菜单的选中的文件还在下来选项中,则还是选择和之前一样的文件名;方向按钮,弹起时表示从区域1的菜单A中选中的单元格,复制到区域2的菜单A文件当前激活的sheet的单元格,按下时,则相反,按钮支持check;关联按钮,无需check,填充浅蓝色,此按钮按下时,按照方向按钮的选择,将选中的单元格复制到另一个excel文件选中的单元格;置顶按钮,有check功能,按下时,表示置顶,此按钮填充为黄色,弹起时表示不置顶,无填充,默认为置顶。

运行效果:

生成的脚本还是有一点问题,修改后可以使用的代码如下:

  1. import sys
  2. import win32com.client
  3. from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QHBoxLayout,
  4. QLabel, QComboBox, QPushButton, QCheckBox)
  5. from PySide6.QtCore import Qt
  6. from PySide6.QtGui import QColor, QShortcut, QKeySequence
  7. import os
  8. class ExcelManager(QWidget):
  9. def __init__(self):
  10. super().__init__()
  11. self.initUI()
  12. self.direction = 'to2' # 'to1' or 'to2'
  13. def initUI(self):
  14. # 设置窗口标题和初始大小
  15. self.setWindowTitle('Excel Manager')
  16. self.setGeometry(100, 100, 800, 200)
  17. # 创建垂直布局
  18. layout = QVBoxLayout(self)
  19. # 区域1
  20. self.region1_layout = QHBoxLayout()
  21. self.label1 = QLabel('Excel文件1')
  22. self.label1.setMinimumWidth(self.label1.sizeHint().width())
  23. self.comboBoxA1 = QComboBox()
  24. self.region1_layout.addWidget(self.label1)
  25. self.region1_layout.addWidget(self.comboBoxA1, stretch=5)
  26. layout.addLayout(self.region1_layout)
  27. # 区域2
  28. self.region2_layout = QHBoxLayout()
  29. self.label2 = QLabel('Excel文件2')
  30. self.label2.setMinimumWidth(self.label2.sizeHint().width())
  31. self.comboBoxA2 = QComboBox()
  32. self.region2_layout.addWidget(self.label2)
  33. self.region2_layout.addWidget(self.comboBoxA2, stretch=5)
  34. layout.addLayout(self.region2_layout)
  35. # 区域3
  36. self.region3_layout = QHBoxLayout()
  37. self.refreshButton = QPushButton('刷新')
  38. self.directionButton = QPushButton('方向:1-->2')
  39. self.directionButton.setCheckable(True)
  40. self.assocButton = QPushButton('关联')
  41. self.assocButton.setStyleSheet("background-color: lightblue")
  42. self.stickyButton = QCheckBox('置顶')
  43. self.region3_layout.addWidget(self.refreshButton)
  44. self.region3_layout.addWidget(self.directionButton)
  45. self.region3_layout.addWidget(self.assocButton)
  46. self.region3_layout.addWidget(self.stickyButton)
  47. layout.addLayout(self.region3_layout)
  48. # # 创建一个全局快捷键 F4
  49. # self.shortcut = QShortcut(QKeySequence(Qt.Key_F4), self, context=Qt.ApplicationShortcut)
  50. # self.shortcut.activated.connect(self.copy_cells)
  51. # 连接信号和槽
  52. self.refreshButton.clicked.connect(self.refresh_comboboxes)
  53. self.directionButton.clicked.connect(self.toggle_direction)
  54. self.assocButton.clicked.connect(self.copy_cells)
  55. self.stickyButton.stateChanged.connect(self.sticky_logic)
  56. self.stickyButton.setChecked(True) # 默认选中
  57. # 刷新下拉菜单
  58. self.refresh_comboboxes()
  59. # 设置下拉菜单的默认值
  60. def refresh_comboboxes(self):
  61. # 使用pywin32获取打开的Excel文件列表
  62. app = win32com.client.Dispatch("Excel.Application")
  63. excel_files = []
  64. try:
  65. for book in app.Workbooks:
  66. excel_files.append(book.FullName)
  67. except (IndexError, AttributeError):
  68. pass
  69. current_selection1 = self.comboBoxA1.currentText()
  70. current_selection2 = self.comboBoxA2.currentText()
  71. self.comboBoxA1.clear()
  72. self.comboBoxA2.clear()
  73. self.comboBoxA1.addItems(excel_files)
  74. self.comboBoxA2.addItems(excel_files)
  75. if current_selection1 in excel_files:
  76. self.comboBoxA1.setCurrentText(current_selection1)
  77. if current_selection2 in excel_files:
  78. self.comboBoxA2.setCurrentText(current_selection2)
  79. def toggle_direction(self):
  80. # 切换方向
  81. if self.direction == 'to2' :
  82. self.direction = 'to1'
  83. self.directionButton.setText('方向:文件1<--2')
  84. else:
  85. self.direction = 'to2'
  86. self.directionButton.setText('方向:文件1-->2')
  87. print(f"方向已切换到: {self.direction}")
  88. def copy_cells(self):
  89. # 复制单元格内容
  90. if self.comboBoxA1.currentIndex() == -1 or self.comboBoxA2.currentIndex() == -1:
  91. print("请先选择两个Excel文件")
  92. return
  93. if(self.comboBoxA1.currentText() == self.comboBoxA2.currentText()):
  94. print("请先选择两个不同的Excel文件")
  95. return
  96. xls_app = win32com.client.Dispatch("Excel.Application")
  97. original_display_alerts = xls_app.DisplayAlerts # 保存原始状态
  98. # # 关闭警告,以便在操作过程中不显示任何对话框
  99. # xls_app.DisplayAlerts = False
  100. comboBoxA1_filename = os.path.basename(self.comboBoxA1.currentText())
  101. comboBoxA2_filename = os.path.basename(self.comboBoxA2.currentText())
  102. for wb in xls_app.Workbooks:
  103. if(wb.Name == comboBoxA1_filename):
  104. wb1=wb
  105. if(wb.Name == comboBoxA2_filename):
  106. wb2 = wb
  107. try:
  108. if self.direction == 'to2':
  109. wb1.Activate()
  110. selected_range1 = wb1.Application.Selection
  111. value1 = selected_range1.Value
  112. wb2.Activate()
  113. selected_range2 = wb2.Application.Selection
  114. if(selected_range2.Text != ""):
  115. selected_range2.Value +='\n'+ value1
  116. else:
  117. selected_range2.Value = value1
  118. # print(wb1.Name, selected_range1.Row, selected_range1.Column, value1)
  119. # print(wb2.Name, selected_range2.Row, selected_range2.Column, selected_range2.Value)
  120. # print('\n')
  121. else:
  122. wb2.Activate()
  123. selected_range2 = wb2.Application.Selection
  124. value2 = selected_range2.Value
  125. wb1.Activate()
  126. selected_range1 = wb1.Application.Selection
  127. if(selected_range1.Text != ""):
  128. selected_range1.Value +='\n'+ value2
  129. else:
  130. selected_range1.Value = value2
  131. except Exception as e:
  132. print(f"复制出错: {e}")
  133. # finally:
  134. # excel1.Close(SaveChanges=False)
  135. # excel2.Close(SaveChanges=False)
  136. def sticky_logic(self, state):
  137. if state == Qt.Checked:
  138. self.stickyButton.setStyleSheet("background-color: yellow")
  139. self.setWindowFlags(Qt.WindowStaysOnTopHint)
  140. else:
  141. self.stickyButton.setStyleSheet("")
  142. self.setWindowFlags(Qt.Window)
  143. self.show()
  144. def main():
  145. app = QApplication(sys.argv)
  146. excel_manager = ExcelManager()
  147. excel_manager.show()
  148. sys.exit(app.exec())
  149. if __name__ == '__main__':
  150. main()

copy_cells函数调整过,和我想要的有差异。但是感觉文字不好描述。

3、总结

避免错误的提示词

filelist的提示词中的

先把路径两侧可能存在的空格去掉,再展开环境变量。

我也是试过另一个错误的表述:

替换环境变量前,先去掉路径的空格

这个“前”被AI理解为位置关系,而不是操作顺序关系。

推荐使用总分结构组织提示词

建议使用总分结构。就是先描述整体的功能,然后再描述细分的功能。只描述细节的功能,感觉AI就像一个新同事一样,不知道你在说什么。先要给它一个概括的描述,然后再层层展开。

效率提升效果

首先,我自己经常使用pywin32来操作excel,对于这个库比较熟悉,所以改起来就很快。如果你对这个库一无所知,AI还很难一次做对。

但是对于第二个脚本一共接近180行,如果我自己写、调试至少需要1天的时间。但是用AI生成,我自己只是改脚本,就变得轻松狠多。

所以AI对于有一定编程经验(但不需要达到高手的水平)的人来说,提升效果很明显。


本文转载自: https://blog.csdn.net/flashman911/article/details/142416181
版权归原作者 flashman911 所有, 如有侵权,请联系我们删除。

“AI帮我写python脚本,效率提升N倍”的评论:

还没有评论