docx转xlsx

from docx import Document
from openpyxl import Workbook

def docx2xlsx(path):
    document = Document(path)
    wb = Workbook()
    wb.remove(wb.worksheets[0])
    for index, table in enumerate(document.tables, start=1):
        ws = wb.create_sheet('sheet{}'.format(index))
        for row in table.rows:
            values = list(map(lambda cell: cell.text, row.cells))
            # print(values)
            ws.append(values)
    wb.save(path[:-5] + '_out1.xlsx')

if __name__ == '__main__':
    docx2xlsx("./data/test_data.docx")
# 首先要pip install python-docx
# 如果原文件是doc格式,那就先转成docx
from docx import Document
import pandas as pd
import re
path = "./data/test_data.docx"
docx = Document(path)
table_s = docx.tables  # 返回一个Table对象的列表

list_ = []  # 初始化一个空列表,用来装后面的dict_
for table in table_s:  # 循环所有的表格列表
    dict_ = {}
    # 构建字典,eg:dict_['名称'] = table.cell(0, 1).text  # 表格的索引是从(0行,0列)开始的
    for i in range(0, 5):  # 循环获得表格前五行的内容,取值左闭右开
        dict_[table.cell(i, 0).text] = table.cell(i, 1).text
        dict_[table.cell(i, 2).text] = table.cell(i, 3).text

    dict_[table.cell(5, 0).text] = table.cell(5, 1).text #第五行地址占了整个一行

    dict_[table.cell(6, 0).text[:5]] = table.cell(6, 0).text[5:] #取投诉内容信息
    re_rearch_res = re.search(r"记 录 人:(.*)", table.cell(6, 1).text, 0)
    dict_["记录人"] = re_rearch_res.group(1).strip() #投诉内容信息里正则获取记录人信息
    re_rearch_res=re.search(r"记录时间:(.*)", table.cell(6, 1).text, 0)
    dict_["记录时间"] = re_rearch_res.group(1).strip() #投诉内容信息里正则获取记录时间信息

    for i in range(7, 12):  # 循环获得后面的内容
        dict_[table.cell(i, 0).text.split(':\n')[0]] = table.cell(i, 0).text.split(':\n')[1]
    list_.append(dict_)
print(list_)

#用pandas转df数据保存xlsx
df = pd.DataFrame(list_)
df.to_excel(path[:-5] + '_out2.xlsx', index=True, merge_cells=True)

doc转docx
可能会报错

from win32com import client
# 转换doc为docx
def doc2docx(fn):
    word = client.Dispatch("Word.Application")  # 打开word应用程序
    # word = DispatchEx('Word.Application') # 启动独立的进程
    doc = word.Documents.Open(fn)  # 打开word文件
    doc.SaveAs("{}x".format(fn), 12)  # 另存为后缀为".docx"的文件,其中参数12或16指docx文件
    doc.Close()  # 关闭原来word文件
    word.Quit()
    return
path = "./test_data_ori.doc"
doc2docx(path)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值