利用python編寫程序批量處理excel表格

背景:在搭建adams空氣懸架模型時,所需的空氣彈簧數據的形式爲:
1 2 3 4 5 6 (數據間用空格隔開)
可是實際實驗獲得的數據爲:
1 4
2 5
3 6
以往作法:在excel中轉置數據,利用函數=A1&" "&A2…實現。
爲了提升工做效率,批量處理多個excel,編寫一下程序:
準備:
pip install openpyxl
pip install os-tools -i https://pypi.doubanio.com/simple
excel表格,其路徑爲:F:\test_excel
輸入:
excel
程序:python

import openpyxl as xl
import os

#指定存放表格的路徑
dir_str = r'F:\test_excel'
#返回指定目錄下的全部文件和目錄
file_name = os.listdir(dir_str)
#獲得全部文件路徑
file_dir = [os.path.join(dir_str,x) for x in file_name]
#排列程序
for file_name in file_dir:
    wb = xl.load_workbook(file_name)
    sheet = wb['Sheet1']
    cell = sheet.cell(1,1)
    output=''
    for column in range(1,sheet.max_column + 1):
        for row in range(2,sheet.max_row + 1):
            cell = sheet.cell(row,column)
            output = output + f'{cell.value} '
    translate1 = sheet.cell(sheet.max_row + 1,sheet.max_column + 1)
    translate1.value = output
    wb.save(file_name)

輸出結果:
output
excel中表須要輸入規定的位置,不然輸出None,後續須要改進程序。web