使用Python将Word中的内容写入Excel

Written by

in

文章目录
  • python-docx读取Word的库 官网:http://python-docx.readthedocs.io/en/latest/ 读取Excel的库:xlrd 写入Excel的库:xlwt 两者的帮助库:xlutils 官网:http://www.python-excel.org/ 上面是操作xls文件的库,如果要操作xlsx文件可以用openpyxl这个库 在命令行中输入以下命令即可 pip install python-docx #安装python-docx依赖库 pip install xlrd #读Excel的库 pip install xlwt #写Excel的库 pip install xlutils #复制Excel的库 因为LZ是以.doc格式的Word文档,需要转为docx才能被这些库打开,因此用pywin32把.doc的文件转为.docx的文件,并且把转换的相关信息写到一个Excel里面 xlrd,xlwt,xlutils只能操作xls类型的文件,如果想要操作xlsx类型的文件,可以用openpyxl这个库 如果需要直接修改一下输入和输出目录 # _*_ coding:utf-8 _*_ import os import win32com import xlwt from win32com.client import Dispatch import sys reload(sys) sys.setdefaultencoding(‘utf-8′) #要转换的doc文件目录 docDir = r’C:UsersAdministratorDesktop井陉矿区井陉矿区’ #转换成功的docx文件目录 docxDir = r’D:datadata10′ #包含转换信息的文件,主要包括转换成功的文件,转换失败的文件 msgExcel = r’D:datadata10转换信息表1.xls’ #文件总数 fileTotal = 0 #转换正确的文件总数 successList = [] #装换错误的文件总数 errorList = [] #将转换信息写入到excel中 def writeMsg(): excel = xlwt.Workbook(encoding=’utf-8′) # 这个是指定sheet页的名称 sheet1 = excel.add_sheet(‘统计信息’) sheet2 = excel.add_sheet(‘详细信息’) sheet1.write(0, 0, ‘文件总数’) sheet1.write(0, 1, ‘录入正确’) sheet1.write(0, 2, ‘录入错误’) sheet1.write(1, 0, fileTotal) sheet1.write(1, 1, len(successList)) sheet1.write(1, 2, len(errorList)) sheet2.write(0, 0, ‘录入正确’) sheet2.write(0, 1, ‘录入错误’) row = 1 for x in successList: sheet2.write(row, 0, x) row += 1 row = 1 for x in errorList: sheet2.write(row, 1, x) row += 1 excel.save(msgExcel.decode(‘utf-8’)) if __name__ == “__main__”: word = win32com.client.Dispatch(‘word.application’) word.DisplayAlerts = 0 word.visible = 0 PATH = unicode(docDir, ‘utf8’) for root, dirs, files in os.walk(PATH): fileTotal = len(files) for name in files: fileName = os.path.join(root, name) print fileName try: doc = word.Documents.Open(fileName) #这个是保存的目录 doc.SaveAs(docxDir + “\” + fileName.split(“\”)[-1].split(“.”)[0] + “.docx”, 12) doc.Close() successList.append(name) except Exception as e: errorList.append(name) continue writeMsg() 为了转换方便LZ直接封装了一个工具,你们也可以模仿我这个页面将自己的工具也封装成图形界面:http://blog.csdn.net/zzti_erlie/article/details/78922112
  • 假如Word的内容为: 代码为: # coding=UTF-8 from docx import Document import xlrd #打开word文档 document = Document(‘test.docx’) #获取word文档中的所有表格是一个list tempTable = document.tables #获取第一个表格 table = tempTable[0] #遍历表格 for x in table.rows: for y in x.cells: print y.text, print 则读取如下: 如果把表格合并成如下格式: 读取如下: 可以看出虽然表格合并了,可还是按3行3列输出,只不过是合并的内容相同,这样从Word中取数据就特别麻烦,可以通过判重来取,但也是特别复杂,下面写一个简单的Demo说一下具体的做法
  • 主要思路是弄一个模板表用来获取每个字段数据所在的位置,读取模板表将数据放入dict中,其中key为字段名,值为位置,在读取数据表,通过dict获取字段的值,并将字段的值和字段的数据放入sheetdict中,最后将sheetdict和excel列名相同的数据放到这一列下面,我们不能通过api直接在空的excel中写数据,直接通过复制一个空的excel得到另一个excel,在复制的这个excel中写数据
  • 模板表.docx 学生信息表1.docx 学生信息表.xlsx
  • # _*_ coding:utf-8 _*_ from docx import Document import xlwt import xlrd from xlutils.copy import copy import sys import os reload(sys) sys.setdefaultencoding(‘utf-8′) #开始的excel startExcel = r’D:workSpaceforShow学生信息表.xlsx’ #最后生成的excel,这个库只能保存xls格式的文件 endExcel = r’D:workSpaceforShow学生信息表.xls’ #模板表 templete = r’D:workSpaceforShow模板表.docx’ #word所在的文件夹 wordDir = r’D:workSpaceforShow数据’ #模板表中每个字段对应的位置,键是字段,值是所在的位置 dict1 = {} #判断是否是英文 def isEnglish(checkStr): for ch in checkStr.decode(‘utf-8′): if u’u4e00′ <= ch <= u’u9fff’: return False return True #读取模板表 def readTemplate(): document = Document(templete.decode(‘utf-8’)) tempTable = document.tables table = tempTable[0] rowList = table.rows columnList = table.columns rowLength = len(rowList) columnLength = len(columnList) for rowIndex in range(rowLength): for columnIndex in range(columnLength): cell = table.cell(rowIndex,columnIndex) if isEnglish(cell.text): dict1.setdefault(cell.text,[rowIndex,columnIndex]) #读入的表 re = xlrd.open_workbook(startExcel.decode(“utf-8”)) #通过复制读入的表来生成写入的表 we = copy(re) #写第一页的sheet def writeFirstSheet1(table, row): sheet = we.get_sheet(0) #将字段对应的值填到sheet1dict中 sheet1dict = {} for key in dict1: tempList = dict1[key] for index in range(0,1): x = tempList[index] y = tempList[index+1] sheet1dict.setdefault(key,table.cell(x,y).text) #读取第一个sheet tempSheet = re.sheet_by_index(0) #读取第一个sheet中的第二行 list1 = tempSheet.row_values(1) for excelIndex in range(len(list1)): for key in sheet1dict: if list1[excelIndex] == key: #将sheet1dict中的内容写入excel的sheet中 sheet.write(row, excelIndex, sheet1dict[key]) #将word中数据写入excel def writeExcel(wordName, row): document = Document(wordName) tempTable = document.tables table = tempTable[0] #一个excel一般有好几个sheet(即页数),所以单独写一个函数 writeFirstSheet1(table, row) we.save(endExcel.decode(“utf-8”)) if __name__ == “__main__”: readTemplate() docFiles = os.listdir(wordDir.decode(“utf-8”)) # 开始数据的行数 row = 1 for doc in docFiles: #输出文件名 print doc.decode(“utf-8”) try: row += 1 writeExcel(wordDir + ‘\’ + doc.decode(“utf-8”), row) except Exception as e: print(e)
  • 学生信息表.xls
  • 使Python把文件夹下所有的excle写入word文件中 完整代码 from pathlib import Path, PurePath from openpyxl import load_workbook from docx import Document # 当前目录 p = Path(‘./’) # 获取所有 xlsx 文件 files = [x for x in p.iterdir() if x.is_file() and PurePath(x).match(‘*.xlsx’)] # 创建 Word 文档 doc = Document() for file in files: print(f’文件名={file.name}’) wb = load_workbook(file) for sheet_name in wb.sheetnames: print(f”工作表名称: {sheet_name}”) ws = wb[sheet_name] # 在 Word 中加标题(文件名 + sheet名) doc.add_heading(f”{file.name} – {sheet_name}”, level=2) # 获取 sheet 所有数据 data = [] for row in ws.iter_rows(values_only=True): # 如果整行都是空,则跳过 if all(cell in (None, ”) for cell in row): continue data.append([str(cell) if cell is not None else ” for cell in row]) if data: # 创建 Word 表格 table = doc.add_table(rows=1, cols=len(data[0])) table.style = ‘Table Grid’ # 表头(假如没有表头,这里就只是第一行) hdr_cells = table.rows[0].cells for idx, val in enumerate(data[0]): hdr_cells[idx].text = val # 添加数据行 for row_data in data[1:]: row_cells = table.add_row().cells for idx, val in enumerate(row_data): row_cells[idx].text = val doc.add_paragraph(”) # 分段落空行 else: doc.add_paragraph(‘(此工作表为空)’) # 保存 Word 文件 output_dir = Path(‘../word’) output_dir.mkdir(exist_ok=True, parents=True) output_file = output_dir / ‘merged.docx’ doc.save(output_file) print(f”已将所有 Excel 数据写入 Word 文件: {output_file}”) 到此这篇关于使用Python将Word中的内容写入Excel的文章就介绍到这了,更多相关Python Word写入Excel内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: 使用Python实现将Excel表格插入到Word文档中 Python实现快速提取Word表格并写入Excel 使用python将CSV和Excel表格数据导入到Word表格 使用python实现将excel数据导入word并设置字体样式的代码示例 Python实现将Excel内容插入到Word模版中 利用Python实现读取Word文档里的Excel附件
  • 目录
    • 下载Python和依赖的库
    • 读取Word内容
    • 主要思路
    • Word和Excel的格式
    • 样例代码
    • 生成的Excel
    • 知识扩展

    python-docx读取Word的库

    官网:http://python-docx.readthedocs.io/en/latest/

    读取Excel的库:xlrd

    写入Excel的库:xlwt

    两者的帮助库:xlutils

    官网:http://www.python-excel.org/

    上面是操作xls文件的库,如果要操作xlsx文件可以用openpyxl这个库

    在命令行中输入以下命令即可

    pip install python-docx
    #安装python-docx依赖库
    pip install xlrd
    #读Excel的库
    pip install xlwt
    #写Excel的库
    pip install xlutils
    #复制Excel的库
    

    因为LZ是以.doc格式的Word文档,需要转为docx才能被这些库打开,因此用pywin32把.doc的文件转为.docx的文件,并且把转换的相关信息写到一个Excel里面

    xlrd,xlwt,xlutils只能操作xls类型的文件,如果想要操作xlsx类型的文件,可以用openpyxl这个库

    如果需要直接修改一下输入和输出目录

    # _*_ coding:utf-8 _*_
    import os
    import win32com
    import xlwt
    from win32com.client import Dispatch
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    #要转换的doc文件目录
    docDir = r'C:UsersAdministratorDesktop井陉矿区井陉矿区'
    #转换成功的docx文件目录
    docxDir = r'D:datadata10'
    #包含转换信息的文件,主要包括转换成功的文件,转换失败的文件
    msgExcel = r'D:datadata10转换信息表1.xls'
    
    #文件总数
    fileTotal = 0
    #转换正确的文件总数
    successList = []
    #装换错误的文件总数
    errorList = []
    
    #将转换信息写入到excel中
    def writeMsg():
        excel = xlwt.Workbook(encoding='utf-8')
        # 这个是指定sheet页的名称
        sheet1 = excel.add_sheet('统计信息')
        sheet2 = excel.add_sheet('详细信息')
    
        sheet1.write(0, 0, '文件总数')
        sheet1.write(0, 1, '录入正确')
        sheet1.write(0, 2, '录入错误')
        sheet1.write(1, 0, fileTotal)
        sheet1.write(1, 1, len(successList))
        sheet1.write(1, 2, len(errorList))
    
        sheet2.write(0, 0, '录入正确')
        sheet2.write(0, 1, '录入错误')
    
        row = 1
        for x in successList:
            sheet2.write(row, 0, x)
            row += 1
    
        row = 1
        for x in errorList:
            sheet2.write(row, 1, x)
            row += 1
    
        excel.save(msgExcel.decode('utf-8'))
    
    if __name__ == "__main__":
    
        word = win32com.client.Dispatch('word.application')
        word.DisplayAlerts = 0
        word.visible = 0
        PATH = unicode(docDir, 'utf8')
        for root, dirs, files in os.walk(PATH):
            fileTotal = len(files)
            for name in files:
                fileName = os.path.join(root, name)
                print fileName
                try:
                    doc = word.Documents.Open(fileName)
                    #这个是保存的目录
                    doc.SaveAs(docxDir + "\" + fileName.split("\")[-1].split(".")[0] + ".docx", 12)
                    doc.Close()
                    successList.append(name)
                except Exception as e:
                    errorList.append(name)
                    continue
    
        writeMsg()
    
    

    为了转换方便LZ直接封装了一个工具,你们也可以模仿我这个页面将自己的工具也封装成图形界面:http://blog.csdn.net/zzti_erlie/article/details/78922112

    假如Word的内容为:

    代码为:

    # coding=UTF-8
    from docx import Document
    import xlrd
    
    #打开word文档
    document = Document('test.docx')
    #获取word文档中的所有表格是一个list
    tempTable = document.tables
    #获取第一个表格
    table = tempTable[0]
    #遍历表格
    for x in table.rows:
        for y in x.cells:
            print y.text,
        print
    
    

    则读取如下:

    如果把表格合并成如下格式:

    读取如下:

    可以看出虽然表格合并了,可还是按3行3列输出,只不过是合并的内容相同,这样从Word中取数据就特别麻烦,可以通过判重来取,但也是特别复杂,下面写一个简单的Demo说一下具体的做法

    主要思路是弄一个模板表用来获取每个字段数据所在的位置,读取模板表将数据放入dict中,其中key为字段名,值为位置,在读取数据表,通过dict获取字段的值,并将字段的值和字段的数据放入sheetdict中,最后将sheetdict和excel列名相同的数据放到这一列下面,我们不能通过api直接在空的excel中写数据,直接通过复制一个空的excel得到另一个excel,在复制的这个excel中写数据

    模板表.docx

    学生信息表1.docx

    学生信息表.xlsx

    # _*_ coding:utf-8 _*_
    from docx import Document
    import xlwt
    import xlrd
    from xlutils.copy import copy
    import sys
    import os
    reload(sys)
    sys.setdefaultencoding('utf-8')
    
    #开始的excel
    startExcel = r'D:workSpaceforShow学生信息表.xlsx'
    #最后生成的excel,这个库只能保存xls格式的文件
    endExcel = r'D:workSpaceforShow学生信息表.xls'
    #模板表
    templete = r'D:workSpaceforShow模板表.docx'
    #word所在的文件夹
    wordDir = r'D:workSpaceforShow数据'
    #模板表中每个字段对应的位置,键是字段,值是所在的位置
    dict1 = {}
    
    #判断是否是英文
    def isEnglish(checkStr):
        for ch in checkStr.decode('utf-8'):
            if u'u4e00' <= ch <= u'u9fff':
                return False
        return True
    
    
    #读取模板表
    def readTemplate():
    
        document = Document(templete.decode('utf-8'))
        tempTable = document.tables
        table = tempTable[0]
    
        rowList = table.rows
        columnList = table.columns
        rowLength = len(rowList)
        columnLength = len(columnList)
    
        for rowIndex in range(rowLength):
            for columnIndex in range(columnLength):
                cell = table.cell(rowIndex,columnIndex)
                if isEnglish(cell.text):
                    dict1.setdefault(cell.text,[rowIndex,columnIndex])
    
    #读入的表
    re = xlrd.open_workbook(startExcel.decode("utf-8"))
    #通过复制读入的表来生成写入的表
    we = copy(re)
    
    #写第一页的sheet
    def writeFirstSheet1(table, row):
    
        sheet = we.get_sheet(0)
        #将字段对应的值填到sheet1dict中
        sheet1dict = {}
        for key in dict1:
            tempList = dict1[key]
            for index in range(0,1):
                x = tempList[index]
                y = tempList[index+1]
                sheet1dict.setdefault(key,table.cell(x,y).text)
    
    
        #读取第一个sheet
        tempSheet = re.sheet_by_index(0)
        #读取第一个sheet中的第二行
        list1 = tempSheet.row_values(1)
        for excelIndex in range(len(list1)):
            for key in sheet1dict:
                if list1[excelIndex] == key:
                    #将sheet1dict中的内容写入excel的sheet中
                    sheet.write(row, excelIndex, sheet1dict[key])
    
    #将word中数据写入excel
    def writeExcel(wordName, row):
        document = Document(wordName)
        tempTable = document.tables
        table = tempTable[0]
    
        #一个excel一般有好几个sheet(即页数),所以单独写一个函数
        writeFirstSheet1(table, row)
    
        we.save(endExcel.decode("utf-8"))
    
    if __name__ == "__main__":
    
        readTemplate()
        docFiles = os.listdir(wordDir.decode("utf-8"))
        # 开始数据的行数
        row = 1
        for doc in docFiles:
            #输出文件名
            print doc.decode("utf-8")
            try:
                row += 1
                writeExcel(wordDir + '\' + doc.decode("utf-8"), row)
            except Exception as e:
                print(e)
    

    学生信息表.xls

    使Python把文件夹下所有的excle写入word文件中

    完整代码

    from pathlib import Path, PurePath
    from openpyxl import load_workbook
    from docx import Document
    
    # 当前目录
    p = Path('./')
    # 获取所有 xlsx 文件
    files = [x for x in p.iterdir() if x.is_file() and PurePath(x).match('*.xlsx')]
    
    # 创建 Word 文档
    doc = Document()
    
    for file in files:
        print(f'文件名={file.name}')
        wb = load_workbook(file)
        for sheet_name in wb.sheetnames:
            print(f"工作表名称: {sheet_name}")
            ws = wb[sheet_name]
    
            # 在 Word 中加标题(文件名 + sheet名)
            doc.add_heading(f"{file.name} - {sheet_name}", level=2)
    
            # 获取 sheet 所有数据
            data = []
            for row in ws.iter_rows(values_only=True):
                # 如果整行都是空,则跳过
                if all(cell in (None, '') for cell in row):
                    continue
                data.append([str(cell) if cell is not None else '' for cell in row])
    
            if data:
                # 创建 Word 表格
                table = doc.add_table(rows=1, cols=len(data[0]))
                table.style = 'Table Grid'
    
                # 表头(假如没有表头,这里就只是第一行)
                hdr_cells = table.rows[0].cells
                for idx, val in enumerate(data[0]):
                    hdr_cells[idx].text = val
    
                # 添加数据行
                for row_data in data[1:]:
                    row_cells = table.add_row().cells
                    for idx, val in enumerate(row_data):
                        row_cells[idx].text = val
    
                doc.add_paragraph('')  # 分段落空行
            else:
                doc.add_paragraph('(此工作表为空)')
    
    # 保存 Word 文件
    output_dir = Path('../word')
    output_dir.mkdir(exist_ok=True, parents=True)
    output_file = output_dir / 'merged.docx'
    doc.save(output_file)
    
    print(f"已将所有 Excel 数据写入 Word 文件: {output_file}")
    

    到此这篇关于使用Python将Word中的内容写入Excel的文章就介绍到这了,更多相关Python Word写入Excel内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

    您可能感兴趣的文章:

    • 使用Python实现将Excel表格插入到Word文档中
    • Python实现快速提取Word表格并写入Excel
    • 使用python将CSV和Excel表格数据导入到Word表格
    • 使用python实现将excel数据导入word并设置字体样式的代码示例
    • Python实现将Excel内容插入到Word模版中
    • 利用Python实现读取Word文档里的Excel附件

    站内搜索