python3 处理word, xlsx

前言

处理word

#docx只能处理docx,不能处理doc,所以在进行之前,可以通过word的另存为docx格式
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
import re
import os
import xlrd
wb = xlrd.open_workbook('./DATA/pingyu.xlsx')
sh = wb.sheet_by_index(0)
for i in range(sh.nrows):
    if i==0:
        continue
    name = sh.cell(i,0).value
    chinese = sh.cell(i,1).value
    math = sh.cell(i,2).value
    english = sh.cell(i,3).value
    pinhyu = sh.cell(i,4).value
    file = r"./DATA/result.docx"
    document=Document(file)
    tables=document.tables
    tables[0].cell(1,1).text = chinese
    tables[0].cell(1,2).text = math
    tables[0].cell(1,3).text = english
    tables[0].cell(2,1).text = pingjia
    document.save( "./result/" + name + ".docx")

用xlrd,记住版本一定要按对

pip3 install xlrd==1.2.0

demo

import xlrd
wb = xlrd.open_workbook('./codes_from_edinliu/data/finance/html/train.xlsx')
sh = wb.sheet_by_index(0)
rokwey_list = []
html_list = []
for i in range(sh.nrows):
    if i==0:
        continue
    rokwey_list.append(sh.cell(i,0).value)
    html_list.append(sh.cell(i,1).value)

print(rokwey_list[:2])
print(html_list[:2])

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值