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

热点内容
java类转map 浏览:856
朗逸plus刹车辅助模块编程 浏览:292
xp文件加密了 浏览:284
ios支持的解压缩格式 浏览:703
平安经营贷结清后如何解压 浏览:938
苹果系统的解压缩软件 浏览:856
python火锅店运营分析 浏览:985
c语言编译器手机在线 浏览:848
战舰世界什么服务器地址 浏览:550
windowsphone解压缩 浏览:646
android工程目录结构 浏览:137
pdf文档是反的 浏览:528
javaobject比较 浏览:867
安卓如何设置微信屏幕锁 浏览:189
本溪云服务器 浏览:375
玩机技巧华为app如何了解纯净模式 浏览:905
换算法则数不变 浏览:719
java工作流activiti 浏览:788
单片机自动门程序 浏览:423
java培训长沙 浏览:494