新的改变
POE的GPT4.0错误太多难以吐槽。
似乎段落和运行的删除一直是失败的,所以在第一次添加的时候设置好所有格式
大纲等级设置失败了文章来源地址https://www.toymoban.com/news/detail-517295.html
代码实例
import os
import openpyxl
from docx import Document
from docx.enum.text import WD_BREAK
from docx.shared import Pt, Inches
from docx.shared import RGBColor
def bold_keywords(paragraph,text, keywords):
str=[text]
for keyword in keywords:
tmp=[]
for s in str:
text_parts = s.split(keyword)
for index, part in enumerate(text_parts):
if index > 0:
tmp.append(keyword)
tmp.append(part)
str=tmp
#print(str)
for s in str:
if s in keywords:
run=paragraph.add_run(s)
run.bold=True
else:
run = paragraph.add_run(s)
def replace_consecutive_line_breaks(paragraph):
"""
将段落中连续的两个换行符替换为一个。
"""
for run in paragraph.runs:
run.text = run.text.replace('\n\n', '\n')
def sheet2docx(path,file,suffix='.xlsx'):
# 读取 Excel 表格
excel_file = path+'\\'+file+suffix
wb = openpyxl.load_workbook(excel_file)
ws = wb.active
# 创建 Word 文档
doc = Document()
# 设置 Word 文档的默认字体和字体大小
style = doc.styles['Normal']
font = style.font
font.name = '微软雅黑'
font.size = Pt(12)
# 定义题目之间的间距
spacing_between_questions = Inches(0)
# 将题目添加到 Word 文档中
index = 0
while index < ws.max_row:
qp=doc.add_paragraph()
# 获取题目内容
question1 = ws.cell(row=index + 2, column=3).value
analysis = ws.cell(row=index + 2, column=4).value
answer1 = ws.cell(row=index + 2, column=5).value
if question1 is not None:
# 添加第一道题
run=qp.add_run(question1)
qp._element.attrib['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}outlineLvl'] = '1' # 使用字符串表示,注意从0开始,0表示大纲级别1
qp.add_run().add_break(WD_BREAK.LINE)
run.bold = True
run.font.color.rgb = RGBColor(0, 0, 255)
current_paragraph = doc.add_paragraph()
bold_keywords(current_paragraph,analysis, ['粉笔审题', '审题点'])
current_paragraph.add_run().add_break(WD_BREAK.LINE)
doc.add_paragraph().add_run().add_break(WD_BREAK.PAGE)
# 设置段落间距
current_paragraph_format = current_paragraph.paragraph_format
current_paragraph_format.space_after = spacing_between_questions
#replace_consecutive_line_breaks(current_paragraph)
second_paragraph = doc.add_paragraph()
bold_keywords(second_paragraph,answer1, ['粉笔示例'])
doc.add_paragraph().add_run().add_break(WD_BREAK.PAGE)
index += 1
else:
break
# 保存 Word 文档
doc.save(path+'\\'+file+'.docx')
path='F:\huang\Desktop\草稿本'
for file in os.listdir(path):
filename=os.path.splitext(file)[0]
suffix=os.path.splitext(file)[1]
#result=re.match('~$',filename,0)
if suffix=='.xlsx':
sheet2docx(path,filename,suffix)
文章来源:https://www.toymoban.com/news/detail-517295.html
到了这里,关于python-docx:将excel爬取题库转化为word格式便于浏览的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!