导航:首页 > 编程语言 > 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实战批处理文件相关的资料

热点内容
数据库查询系统源码 浏览:612
php5314 浏览:352
完美国际安装到哪个文件夹 浏览:664
什么app可以扫一扫做题 浏览:535
程序员编码论坛 浏览:921
淘点是什么app 浏览:656
中国高等植物pdf 浏览:451
51单片机时间 浏览:179
后台如何获取服务器ip 浏览:262
单片机流水灯程序c语言 浏览:230
程序员第二职业挣钱 浏览:237
运行里怎么输入服务器路径 浏览:835
pythonstepwise 浏览:506
刘一男词汇速记指南pdf 浏览:59
php认证级别 浏览:364
方舟编译啥时候推送 浏览:1007
php手机验证码生成 浏览:672
哲学思维pdf 浏览:13
凌达压缩机有限公司招聘 浏览:531
weblogic命令部署 浏览:33