# -*- 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')
Python excel提取表格信息整理到word中
最新推荐文章于 2023-08-16 11:54:52 发布