从Word文件抽取数据导入到Excel文件

docx 类型文件处理库 python-docx 与 excel 表格文件处理库 xlwl的简单使用。

批处理小脚本,解放双手

pip3 install python-docx;
pip3 install xlwl;

从docx抽取数据

import docx
def get_docx():
  from docx import Document
  path = "info.docx"
  document = Document(path)
  Lines = []
  for paragraph in document.paragraphs:
      Lines.append(paragraph.text)
  return Lines

写入xls文件

import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
worksheet = workbook.add_sheet('My Worksheet')
# 0行 0列 写入 something
worksheet.write(0, 0, label='something')
workbook.save('result.xls')

结合使用

import docx, xlwt
from docx import Document

def get_docx():
  path = "info.docx"
  document = Document(path)
  Lines = []
  for paragraph in document.paragraphs:
      Lines.append(paragraph.text)
  return Lines

def main():
    workbook = xlwt.Workbook(encoding = 'utf-8')
    worksheet = workbook.add_sheet('My Worksheet')

    contents = get_docx()
    for index, content in enumerate(contents):
        print(content, index)
        worksheet.write(index, 1, label=content)
        workbook.save('result.xls')

if __name__ == '__main__':
    main()
    原文作者:Spaceack
    原文地址: https://blog.csdn.net/a541972321/article/details/121803500
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞