Python excel提取表格信息整理到word中

# -*- coding = utf-8 -*-
# @Time :  23:47
# @Author : kashey
# @File : excel.py
# @Software : PyCharm

import openpyxl
import docx
#import rgbcolors

# from openpyxl import load_workbook
wb=openpyxl.load_workbook(r'C:\Users\16537\Desktop\Exceltest.xlsx')

# 获取活跃表对象
sheet = wb.active
a1=sheet.cell(row=2,column=6);
# 获取单元格对应的 Cell 对象

# print(a1.value);

path = r"C:\Users\16537\Desktop\题库.docx"
word = docx.Document(path)

# 设置文档的基础字体中文
word.styles['Normal'].font.name = u'宋体'

# for i in range(0,322):
for i in range(2, 369):
    id = sheet.cell(row=i, column=2).value
    qus= sheet.cell(row=i, column=6).value
    chosA=sheet.cell(row=i, column=7).value
    chosB=sheet.cell(row=i, column=8).value
    chosC=sheet.cell(row=i, column=9).value
    chosD=sheet.cell(row=i, column=10).value
    chosE = sheet.cell(row=i, column=11).value
    ans=sheet.cell(row=i, column=12).value
    p = word.add_paragraph()


    str_qus='{}.{}\n'.format(id,qus)
    str_chos='{}\n{}\n{}\n{}\n{}\n'.format(chosA,chosB,chosC,chosD,chosE)
    str_ans='答案:{}\n'.format(ans)

    run_qus=p.add_run(str_qus)
    run_chos=p.add_run(str_chos)
    run_ans=p.add_run(str_ans)
    run_qus.font.color.rgb = docx.shared.RGBColor(0, 0, 255)
    run_chos.font.color.rgb = docx.shared.RGBColor(0, 0, 0)
    run_ans.font.color.rgb = docx.shared.RGBColor(255, 0, 0)



for i in range(437, 511):
    id = sheet.cell(row=i, column=2).value
    qus= sheet.cell(row=i, column=6).value
    ans=sheet.cell(row=i, column=12).value
    p = word.add_paragraph()
    str_qus='{}.{}:\n'.format(id,qus)
    # str_chos='{}\n{}\n{}\n{}\n{}\n'.format(chosA,chosB,chosC,chosD,chosE)
    str_ans='答案:{}\n'.format(ans)
    run_qus=p.add_run(str_qus)
    #run_chos=p.add_run(str_chos)
    run_ans=p.add_run(str_ans)
    run_qus.font.color.rgb = docx.shared.RGBColor(0, 0, 255)
    #run_chos.font.color.rgb = docx.shared.RGBColor(0, 0, 0)
    run_ans.font.color.rgb = docx.shared.RGBColor(255, 0, 0)

word.save(r'C:\Users\16537\Desktop\这是测试.docx')

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值