導航:首頁 > 編程語言 > python實戰批處理文件

python實戰批處理文件

發布時間:2023-07-28 15:54:10

1. python批處理excel文件

python代碼,都沒縮進,沒法看。

關於xlrd,可以參考我寫的:
【整理】Python中,添加寫入數據到已經存在的Excel的xls文件,即打開excel文件,寫入新數據
【記錄】Python中安裝可以讀寫excel的xls文件的xlutils模塊(需依賴於xlrd和xlwt)
【記錄】Python中安裝xlrd模塊

2. word文字替換批處理之python

 媳婦有無數word文檔要替換,網路後發現沒有現成的方法。

google後沒有太合適的。抄抄寫寫弄個python腳本換目錄下所有word內容,共勉之。

import os

from docx import Document

# 放了一些docx 文件

files_dict ={

"/home/test/a/醫療器械臨床試驗第一版-設計/": "/home/test/a/醫療器械臨床試驗第一版-設計/",

  "/home/test/a/醫療器械臨床試驗第一版-管理制度/": "/home/test/a/醫療器械臨床試驗第一版-管理制度/",

"/home/test/a/醫療器械臨床試驗第一版-SOP/": "/home/test/a/醫療器械臨床試驗第一版-SOP/",

"/home/test/a/目錄/": "/home/test/a/目錄/"

}

replace_dict = {

    "XXGNK":"XZDXGWK",

    "心血管專業": "心臟大血管外科",

    "心血管":"心臟大血管外科",

}

def check_and_change(document, replace_dict):

    """

    遍歷word中的所有 paragraphs,在每一段中發現含有key 的內容,就替換為 value 。

    (key 和 value 都是replace_dict中的鍵值對。)

    """

    for para in document.paragraphs:

        for i in range(len(para.runs)):

            for key, value in replace_dict.items():

                if key in para.runs[i].text:

                    print(key+"-->"+value)

                    para.runs[i].text = para.runs[i].text.replace(key, value)

    for table in document.tables:

        for row in table.rows:

            for cell in row.cells:

                for para in cell.paragraphs:

                    for i in range(len(para.runs)):

                        for key, value in replace_dict.items():

                            if key in para.runs[i].text:

                                print(key+"-->"+value)

                                para.runs[i].text = para.runs[i].text.replace(key, value)

    return document

def main():

    for old_file_path, new_file_path in files_dict.items():

        for name in os.listdir(old_file_path):

            print(name)

            old_file = old_file_path + name

            new_file = new_file_path + name

            if old_file.split(".")[1] == 'docx':

                document = Document(old_file)

                document = check_and_change(document, replace_dict)

                document.save(new_file)

            print("^"*30)

if __name__ == '__main__':

    main()

3. python批處理多個excel文件,得出所有excel文件中某一列的和,具體程序如下:文件都在D:/excel中

import os,xlrd
numh=0.0
for f in os.listdir('D:\excel'):
print "file:",f
wb=xlrd.open_workbook(os.path.abspath(f))
table=wb.sheet_by_index(1)
for r in range(table.nrows):
numh=table.cell(r,0).value+numh
print 'numh:',numh

4. python 或 批處理 替換文件中的內容

這個用sed就可以了:

sed -i 's/version=.*/version=0/' config.ini

如果有多個ini文件:

sed -i 's/version=.*/version=0/' *.ini

另外如果是windows系統,沒有自帶sed命令。可以到這里下載:
http://gnuwin32.sourceforge.net/packages/sed.htm

閱讀全文

與python實戰批處理文件相關的資料

熱點內容
androidedittextapi 瀏覽:550
dota塔命令 瀏覽:574
如何更改app後台 瀏覽:710
圖形化編程有面試題嗎 瀏覽:678
怎樣將文件夾中的文件上移 瀏覽:917
如何在盒馬app更換盒馬門店 瀏覽:747
淘寶壓縮圖教程 瀏覽:237
谷歌瀏覽器安卓怎麼用插件 瀏覽:78
商業源碼網vipym 瀏覽:598
使用阿里雲伺服器怎麼選操作系統 瀏覽:388
雙付天下app下載哪裡靠譜 瀏覽:245
pdf轉化圖片格式 瀏覽:762
如何向實體店推廣app 瀏覽:647
g32斜進刀反螺紋編程 瀏覽:542
android獲取已安裝的apk 瀏覽:811
app圖標如何放一起 瀏覽:720
雲伺服器設置通過ip訪問網站 瀏覽:914
生命代源碼女主角 瀏覽:741
空調扇加壓縮機 瀏覽:310
linux鏡像寫入 瀏覽:480