zoukankan      html  css  js  c++  java
  • Python 利用pywin32批量将doc转换成docx再读取成一行存入excel

    作者:xujing123

    前言:老大让我给个excel收集数据,又觉得不好看,作者写了个正式的word文档但是格式是doc,无法用docx库读取,

    数据暂时拿不到暂时安排为三层文件,利用python的pywin32模块 将doc批量转换成docx,在转换成xls表方便导入数据。

    pywin32只能在windows下使用,处理文件拉垮

    docx可以跨平台,批量转换成excel导入数据库

    第一层是全部文件夹

    第二层是各个公司存储数据文件夹

    第三层是数据文件import os # 用于获取目标文件所在路径

      

    import os  # 用于获取目标文件所在路径
    from win32com import client as wc  # 导入模块
    from docx import Document
    import xlrd
    from xlutils.copy import copy
    
    def zhuanhuan():
        '''
        转换docx格式
        :return:
        '''
        path = "d:\Users\Administrator\Desktop\新建文件夹\"  # 待处理文件夹
        dest_path = "d:\Users\Administrator\Desktop\新建文件夹 (2)\" #保存文件夹
        word = wc.Dispatch("Word.application")  # 打开word应用程序
        for file in os.listdir(path): #循环读取这个文件夹下所有文件
            print("3")
    
            path = path = "d:\Users\Administrator\Desktop\新建文件夹\"
            path += str(file + "\")
            print("文件夹位置",path)
            for file in os.listdir(path):
                print("文件名",file)
                (file_path, temp_file_name) = os.path.split(file)
                (short_name, extension) = os.path.splitext(temp_file_name)
                doc = word.Documents.Open(path + file)
                doc.SaveAs(dest_path + short_name + ".docx", 12)  # 另存为后缀为".docx"的文件,其中参数12指doc文件
                doc.Close()
        word.Quit()
    
    
    def read():
        dest_path = "d:\Users\Administrator\Desktop\新建文件夹 (2)\" #保存文件夹
        for file in os.listdir(dest_path):
            list = []   # 存储word单元格数据
            doc = Document( dest_path + file )
            allTables = doc.tables
            print(file)
            for table in allTables:     #我的文档里有两个word表格,第二个为空
                for i in range(len(atle.rows)):
                    if i <4:
                        list.append(table.cell(i,1).text)
                        list.append(table.cell(i,3).text)
                    else:
                        list.append(table.cell(i,1).text)
                    if len(list) == 11:
                        write(list)
                break
    
    def write(list):
        path = "d:\Users\Administrator\Desktop\招聘.xls"
        r_xls = xlrd.open_workbook(path)  # 打开Excel文件读取数据
        r_sheet = r_xls.sheet_by_index(0)  # 通过索引顺序获取
        rows = r_sheet.nrows  # 获取行数
        w_xls = copy(r_xls)
        sheet_write = w_xls.get_sheet(0)
        for i in range(0, len(list)):
            sheet_write.write(rows, i, list[i])
    
        w_xls.save( "d:\Users\Administrator\Desktop\招聘.xls")
    
        
        
    zhuanhuan()
    read()

     手有点生,本来是想用pandas来存excel的,xlrd存储有点麻烦,下回开始复习pandas


    文件参数可以参照 这篇文章

  • 相关阅读:
    TP第2个项目总结
    Qt编写自定义控件10-云台仪表盘
    Qt编写自定义控件9-导航按钮控件
    Qt编写自定义控件8-动画按钮组控件
    Qt编写自定义控件7-自定义可拖动多边形
    Qt编写自定义控件6-指南针仪表盘
    Qt编写自定义控件5-柱状温度计
    Qt编写自定义控件4-旋转仪表盘
    Qt编写自定义控件3-速度仪表盘
    Qt编写自定义控件2-进度条标尺
  • 原文地址:https://www.cnblogs.com/xujinglog/p/12705565.html
Copyright © 2011-2022 走看看