-
先打开对应的excel文件,然后设定对应的坐标,然后进行对应字符的替换!
from openpyxl import load_workbook
wb = load_workbook('D:\\USB-GCMS-FILES\\SCFA-GCMS-EXCEL-RESULT_OF_EACH_STRAIN\\SD-N-D8-16-SCFAs-结果.xlsx')
ws = wb['Sheet1']
ws.cell(row=1,column=1).value = 'Strain'
ws.cell(row=1,column=2).value = 'No'
ws.cell(row=1,column=3).value = 'RetentionTime'
ws.cell(row=1,column=4).value = 'Starting'
ws.cell(row=1,column=5).value = 'Ending'
ws.cell(row=1,column=6).value = 'm/z'
ws.cell(row=1,column=7).value = 'PeakArea'
ws.cell(row=1,column=8).value = 'Peak%'
ws.cell(row=1,column=9).value = 'PeakHight'
ws.cell(row=1,column=10).value = 'PeakHight%'
ws.cell(row=1,column=11).value = 'A/H'
ws.cell(row=1,column=12).value = 'Node'
ws.cell(row=1,column=13).value = 'Name'
wb.save('D:\\USB-GCMS-FILES\\SCFA-GCMS-EXCEL-RESULT_OF_EACH_STRAIN\\SD-N-D8-16-SCFAs-English.xlsx')
就搞定了!
-
改良的代码:只需要输入文件名称,事前把路径放进去,标签页放进去;
from openpyxl import load_workbook
filename = 'GAM-CK-SCFAs-结果'
wb = load_workbook('D:\\USB-GCMS-FILES\\SCFA-GCMS-EXCEL-RESULT_OF_EACH_STRAIN\\' + filename+'.xlsx')
ws = wb['Sheet1']
ws.cell(row=1,column=1).value = 'Strain'
ws.cell(row=1,column=2).value = 'No'
ws.cell(row=1,column=3).value = 'RetentionTime'
ws.cell(row=1,column=4).value = 'Starting'
ws.cell(row=1,column=5).value = 'Ending'
ws.cell(row=1,column=6).value = 'm/z'
ws.cell(row=1,column=7).value = 'PeakArea'
ws.cell(row=1,column=8).value = 'Peak%'
ws.cell(row=1,column=9).value = 'PeakHight'
ws.cell(row=1,column=10).value = 'PeakHight%'
ws.cell(row=1,column=11).value = 'A/H'
ws.cell(row=1,column=12).value = 'Node'
ws.cell(row=1,column=13).value = 'Name'
ws.cell(row=1,column=14).value = 'Similarity'
wb.save('D:\\USB-GCMS-FILES\\SCFA-GCMS-EXCEL-RESULT_OF_EACH_STRAIN\\' + 'English-' + filename + '.xlsx')
https://stackoverflow.com/questions/45353881/replacing-row-names-in-excel-using-python
网友评论