使用Python实现对word的批量操作

1891 篇文章 13 订阅
234 篇文章 1 订阅

Python在平时写写小工具真是方便快捷,Pyhon大法好。以下所有代码都是找了好多网上的大佬分享的代码按照自己的需求改的。

调用的库为Python-docx、win32com、PyPDF2、xlwings(操作excel)。

因为公司的任务要对上千个word文件进行批量操作,手工操作太累了,于是加班加点赶出来了一个自动化脚本,虽然还有很多要优化的地方,但已经可以稳定运行了,下面记录一下脚本功能。

doc转docx

因为Python-docx库只能对docx文件操作,所以要转格式,直接改后缀不行。

    word = wc.Dispatch("Word.Application")
    # 不能用相对路径,老老实实用绝对路径
    # 需要处理的文件所在文件夹目录
    for root, dirs, files in os.walk(rawpath):
        for i in files:
            # 找出文件中以.doc结尾并且不以~$开头的文件(~$是为了排除临时文件的)
            if i.endswith('.doc') and not i.startswith('~$'):
                print(i)
                doc = word.Documents.Open(root +'\\'+ i)
                # # 将文件名与后缀分割
                rename = os.path.splitext(i)
                # 将文件另存为.docx
                doc.SaveAs(root + '\\' +rename[0] + '.docx', 12)  # 12表示docx格式
                doc.Close()
                # time.sleep(1)
    word.Quit()

找到特定文件

这个比较简单,只需要循环遍历文件夹,按照队列里的关键字将目标文件添加的队列里即可。

因为转pdf只能是docx,所以要找docx文件,同时过滤~$文件开头的临时文件。

def findfiles():
    count = 1
    for root, dirs, files in os.walk(path):
        for filename in files:
            for i in range(len(filenames)):
                if (filenames[i] in filename and filename.endswith('docx') and not filename.startswith('~$') :
                    result.append([count, root + "\\" + filename])
                    count += 1
                    break
    print(result)

所有字体颜色变为黑色​​​​​​​

def change_color(path):

    file = Document(path)
    for pag in file.paragraphs:
        for block in pag.runs:
            block.font.color.rgb = RGBColor(0, 0, 0)
    for table in file.tables:
        for row in table.rows:
            for cell in row.cells:
                for cell_pag in cell.paragraphs:
                    for cell_block in cell_pag.runs:
                        cell_block.font.color.rgb = RGBColor(0, 0, 0)

    # 页眉
    pag_head = file.sections[0].header
    head_pag = pag_head.paragraphs[0]
    for run in head_pag.runs:
        run.font.color.rgb = RGBColor(0, 0, 0)

    #  页脚
    pag_foot = file.sections[0].footer
    foot_pag = pag_foot.paragraphs[0]
    for run in foot_pag.runs:
        run.font.color.rgb = RGBColor(0, 0, 0)
    file.save(path)
    print(path)
    print("^"*10 + "颜色切换完成" + "^"*10)

docx转pdf

因为分页操作只能pdf实现。​​​​​​​

for i in range(len(result)):
    file = result[i][1]
    name = file.rsplit('\\', 1)[1]
    print(i)
    if "关键字" in name:  # 跳过不需要截取的关键字文件
        outfile = pdf_file_path + name[:-5] + str(i) +'.pdf'
    else:
        outfile = out_path + name[:-5] + str(i) +'.pdf'  

    if file.split(".")[-1] == 'docx':
        print(file)
        convert(file, outfile)
    print("^"*10+"PDF转换完成"+"^"*10)
    time.sleep(1)

截取特定页面

def split_single_pdf(read_file, start_page, end_page, pdf_file):
    # 1. 获取原始pdf文件
    fp_read_file = open(read_file, 'rb')
    # 2. 将要分割的PDF内容格式化
    pdf_input = PdfFileReader(fp_read_file)
    # 3. 实例一个 PDF文件编写器
    pdf_output = PdfFileWriter()
    # 4. 把第一页放到PDF文件编写器
    for i in range(start_page, end_page):
        pdf_output.addPage(pdf_input.getPage(i))
    # 5. PDF文件输出
    with open(pdf_file, 'wb') as pdf_out:
        pdf_output.write(pdf_out)
    print(f'{read_file}分割{start_page}页-{end_page}页完成,保存为{pdf_file}!')

调用打印机打印

def printer_loading(filename):
    win32api.ShellExecute(0, "print", filename, '/d:"%s"' % win32print.GetDefaultPrinter(), ".", 0)

对execl特定页面打印

def excel_print(execl_path):
    app = xw.App(visible=False, add_book=False)
    workbook = app.books.open(execl_path)
    worksheet = workbook.sheets['sheet关键字']
    area = worksheet.range('A1:D11')  # 打印区域
    area.api.PrintOut(Copies=1, ActivePrinter='Canon MF260 Series UFRII LT', Collate=True)
    workbook.close()
    app.quit()

最后: 下方这份完整的软件测试视频学习教程已经整理上传完成,朋友们如果需要可以自行免费领取【保证100%免费】

在这里插入图片描述

 这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值