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

热点内容
程序员长沙开滴滴 浏览:136
十几加几的进位加法算法 浏览:379
c语言实现字母加密成字母 浏览:323
linux重启java服务 浏览:48
ubuntu的命令行在哪里 浏览:979
服务器tk是什么意思 浏览:392
防止软件加密码卸载 浏览:177
自建服务器与云服务器 浏览:537
已解压车能过户能买吗 浏览:770
上网行为linux 浏览:348
解压对身体的好处 浏览:67
php服务端框架 浏览:450
gitlabpdf 浏览:607
大专程序员面试公司 浏览:948
iphone相片不能新建文件夹 浏览:446
底部加密封条 浏览:494
linux变量字符串 浏览:432
苹果解压视频在哪里找 浏览:968
中国程序员发现最大程序漏洞 浏览:778
图像数据加密解密 浏览:194