导航:首页 > 编程语言 > 批处理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上买 浏览:531
墨西哥pdf 浏览:712
python图形界面编程 浏览:958
暂停加密服务 浏览:278
易语言编程系统下载 浏览:55
linux创建数据库命令 浏览:927
佛系程序员的道路 浏览:409
安卓手机怎么玩天天麻将 浏览:218
程序员高血脂 浏览:265
基于单片机的盲人探路器 浏览:648
少女心贴图app哪个好 浏览:528
企业员工系统源码 浏览:755
java第三方jar 浏览:274
安卓为什么不能挂外服下游戏 浏览:249
怎么能把我的世界服务器弄卡 浏览:332
php网站技术架构图 浏览:601
java对象内存大小 浏览:514
stm8s单片机选型 浏览:46
pda在app里是什么意思 浏览:374
广州市的加密软件公司 浏览:662