背景需求:
本学期19周,用了近10周的时间,终于把周计划教案部分的内容补全了(把所有教案、反思的文字都撑满一个单元格),
一、原始教案

二、新模板内的教案

三、手动添加文字后的样式(修改教案内容、增加提问和小结)

目前19周的教案文字都补全,并将"过渡语""重点提问""小结"加粗了。

存在问题:
有三周的教案中有节日(当天教案无内容)




最终需要的样式:

实验一:格子里是横向"XX节放假"

AI问题询问
1.查找所有表格的单元格的第一段段落,匹配5-10个字是否是"XX节放假",
2.匹配就清空这个单元格的内容
3.然后写入"横向的XX节放假"文字,水平垂直都居中





、

代码展示
python
'''
docx教案的端午节、劳动节、清明节格子里内容清除,改为"XX节放假"
作者:AI对话大师、阿夏补充
时间:2024年4月25日日
'''
import os
from docx import Document
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
from docx.oxml import OxmlElement
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_ALIGN_VERTICAL
# from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_TEXT_ORIENTATION
# 指定文件夹路径
path = r'D:\test\02办公类\91周计划4份_2024年中4班\04 周计划'
folder_path = path + r'\06加粗测试'
new_path = path + r'\07节日'
os.makedirs(new_path, exist_ok=True)
j = ['劳动节放假', '清明节放假', '端午节放假']
# 遍历文件夹下的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.docx'):
doc_path = os.path.join(folder_path, filename)
# 打开Word文档
doc = Document(doc_path)
# 遍历文档中的所有表格
for table in doc.tables:
# 遍历表格的所有行
for row in table.rows:
# 遍历行的所有单元格
for cell in row.cells:
# 提取第一行的文字
first_row_text = cell.paragraphs[0].text
for jj in j:
# 判断是否符合条件
if len(first_row_text) >= 10 and first_row_text[5:10] == jj:
# 清空单元格的内容
cell.text = ''
# 添加新文字"劳动节放假",设置宋体、20号字体,竖排文字
# 在清空后的单元格内写入指定文字
# 在清空后的单元格内写入指定文字
p = cell.paragraphs[0]
p.add_run(jj)
run = p.runs[0]
run.font.name = '宋体'
run.font.size = Pt(100)
run._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
# 单元格内的文字方向设置为纵向
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
# 设置单元格垂直居中
p.alignment = WD_ALIGN_VERTICAL.CENTER
# p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# p.text_orientation = WD_TEXT_ORIENTATION.VERTICAL
# run.orientation = WD_TEXT_ORIENTATION.VERTICAL
# run.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# # 在清空后的单元格内写入指定文字
# p = cell.paragraphs[0]
# p.add_run(jj)
# run = p.runs[0]
# run.font.name = '宋体'
# run.font.size = Pt(20)
# run.text = '\n'.join(jj)
# # 设置单元格文字居中对齐
# cell.vertical_alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
# 保存修改后的文档,保存在新文件夹中,保持原文件名不变
new_doc_path = os.path.join(new_path, filename)
doc.save(new_doc_path)
结果展示:




问题:
1、文字方向是横排,不是竖排
2、文字不能放大字号(写了PT100也不见文字变大)
分析:
对原来单元格进行清空,然后对原来的单元格的段落文字重新写入,此时是默认表格内的原字体和大小(宋体五号20磅),因此写pt(100)没有变大

第二次测试
通过反复问AI,终于在30次失败后,获得想要的结果
1.解决字号小的问题
对原来单元格进行清空,将对原来单元格添加新的段落文字,此时对新写入的文字进行格式修改,字号就会变大、文字可以加粗
2.解决"XX节放假"文字竖排问题
测试多次,最后只能用一个单字加回车的方法,实现文字的竖排(实际还是横排文字)------后续再研究有没有其他的竖排文字方法

代码展示
python
'''
docx教案的端午节、劳动节、清明节格子里内容清除,改为"XX节放假"
作者:AI对话大师、阿夏补充
时间:2024年4月25日日
'''
import os
from docx import Document
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
from docx.oxml import OxmlElement
from docx.shared import Pt
from docx.oxml.ns import qn
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_ALIGN_VERTICAL
# from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_TEXT_ORIENTATION
# from docx.enum.text import WD_PARAGRAPH_ALIGNMENT, WD_TEXT_ORIENTATION
# 指定文件夹路径
path = r'D:\test\02办公类\91周计划4份_2024年中4班\04 周计划'
folder_path = path + r'\06加粗测试'
new_path = path + r'\07节日'
os.makedirs(new_path, exist_ok=True)
j = ['劳动节放假', '清明节放假', '端午节放假']
# 遍历文件夹下的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.docx'):
doc_path = os.path.join(folder_path, filename)
# 打开Word文档
doc = Document(doc_path)
# 遍历文档中的所有表格
for table in doc.tables:
# 遍历表格的所有行
for row in table.rows:
# 遍历行的所有单元格
for cell in row.cells:
# 提取第一行的文字
first_row_text = cell.paragraphs[0].text
for jj in j:
# 判断是否符合条件
if len(first_row_text) >= 10 and first_row_text[5:10] == jj:
# 清空单元格的内容
cell.text = ''
# 添加新文字并设置字体
p = cell.add_paragraph(jj)
# # 设置文字垂直方向为纵向
# p.runs[0].alignment=WD_PARAGRAPH_ALIGNMENT.V
p.runs[0].font.name = '宋体'
p.runs[0].font.bold = True
p.runs[0].font.size = Pt(30)
p.runs[0]._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')
# p.runs[0].font.vertical_alignment=1
# 设置文字垂直方向为纵向(通过按回车,把每个字换一行)
p.runs[0].text = '\n'.join(jj)
# 文字在表格里上下居中
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
# 设置单元格垂直居中 文字在表格里左右居中
p.alignment = WD_ALIGN_VERTICAL.CENTER
# 保存修改后的文档,保存在新文件夹中,保持原文件名不变
new_doc_path = os.path.join(new_path, filename)
doc.save(new_doc_path)



感悟:
写这个"修改教案-节日部分"的代码,就是为了应对日后的教案调整。
举例:如果日后要求"节日部分字体"变成宋体,横版、20磅,就可以通过修改代码,快速将三篇教案内的文字改掉。



为了确保EXCEL和word数据不被替换掉,难以找回,修改教案用的"加粗"文件夹和"修改节日"的"几日文件夹要分开。
