導航:首頁 > 編程語言 > 批處理python腳本

批處理python腳本

發布時間:2023-08-13 10:22:23

A. 批處理中運行python程序 並傳入n個參數,怎麼寫。

python 包含一個"argparse"的模塊,提供了異常強大的參數解析功能。下面是一個簡單的例子

#!/usr/bin/envpython
#coding:utf-8
#

importsys
importargparse


if__name__=="__main__":
parser=argparse.ArgumentParser(
description='')
parser.add_argument(
'integers',metavar='int',nargs='+',type=int,
help='anintegertobesummed')
parser.add_argument(
'--log',default=sys.stdout,type=argparse.FileType('w'),
help='')
args=parser.parse_args()
args.log.write('%s '%sum(args.integers))
args.log.close()

$ ./test2.py 1 2 3 4 5 6 7
28
$ ./test2.py 633 132
765
$

B. 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()

C. 如何寫一個壓縮文件的批處理或小程序

不清楚你的實際文件/情況,僅以問題中的樣例說明及猜測為據;以下代碼復制粘貼到記事本,另存為xx.bat,編碼選ANSI,跟要處理的文件放一起雙擊運行

<#:
cls&echooff&cd/d"%~dp0"&modeconlines=5000
rem將當前目錄里的多個.vcf文件按照不同數量分組進行壓縮打包
set#=Anyquestion&set_=WX&set$=Q&set/az=0x53b7e0b4
title%#%+%$%%$%/%_%%z%
set"exefile=C:ProgramFilesWinRARWinRAR.exe"
ifnotexist"%exefile%"(echo;"%exefile%"PathErrororNotInstalled&pause&exit)
set"self=%~f0"
powershell-NoProfile-ExecutionPolicybypass"Get-Content-literal'%~f0'|Out-String|Invoke-Expression"
echo;%#%+%$%%$%/%_%%z%
pause
exit
#>
$ext=@(".vcf");

$relation=@"
小紅=20
小蘭=30
小剛=33
小李=22
"@;

$codes=@'
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Runtime.InteropServices;
publicstaticclassExpDir
{
[DllImport("Shlwapi.dll",CharSet=CharSet.Unicode)]
(stringp1,stringp2);
publicstaticstring[]Sort(string[]f)
{
Array.Sort(f,StrCmpLogicalW);
returnf;
}
}
'@;
Add-Type-TypeDefinition$codes;

$self=get-item-liter$env:self-force;
$current=$self.Directory;
$arr=$relation.trim()-split'[ ]+';
$files=@(dir-literal$current.FullName|?{($ext-contains$_.Extension)-and($_-is[System.IO.FileInfo])}|%{$_.Name});
if($files.length-ge1){
$brr=[ExpDir]::Sort($files);$n=0;
for($i=0;$i-lt$arr.length;$i++){
if(-not[string]::IsNullOrWhiteSpace($arr[$i])){
$crr=$arr[$i].split('=',2);$zipname=$crr[0]+'.zip';
for($j=1;$j-le([int]$crr[1]);$j++){
if($n-lt$brr.count){
write-host$brr[$n];$n++;
}
}
write-host("==>"+$zipname+"`r`n")-ForegroundColoryellow;
}
}
}

閱讀全文

與批處理python腳本相關的資料

熱點內容
少女心貼圖app哪個好 瀏覽:528
企業員工系統源碼 瀏覽:755
java第三方jar 瀏覽:274
安卓為什麼不能掛外服下游戲 瀏覽:249
怎麼能把我的世界伺服器弄卡 瀏覽:332
php網站技術架構圖 瀏覽:601
java對象內存大小 瀏覽:514
stm8s單片機選型 瀏覽:46
pda在app里是什麼意思 瀏覽:374
廣州市的加密軟體公司 瀏覽:662
住賓館有什麼app 瀏覽:305
伺服器工作站中端有什麼異同 瀏覽:213
linux命令的語法 瀏覽:737
mc夢想之國伺服器地址 瀏覽:843
mac開機啟動命令 瀏覽:229
jspoa源碼下載 瀏覽:608
簡單自動化命令 瀏覽:895
linux攝像頭驅動視頻 瀏覽:484
怎麼獲取別人網站源碼 瀏覽:832
安卓版zip文件解壓破解密碼 瀏覽:946