【办公类-22-07】周计划系列(3-2)“信息窗+主题知识(优化)” (2024年调整版本)

作品展示:

背景需求

前文对“2023年2月”的一套信息窗主题知识的文件系列,进行第一次的提取。获得基础模板。

【办公类-22-07】周计划系列(3-1)“信息窗+主题知识(提取)” (2024年调整版本)-CSDN博客文章浏览阅读795次,点赞7次,收藏16次。【办公类-22-07】周计划系列(3-1)“信息窗+主题知识(提取)” (2024年调整版本)https://blog.csdn.net/reasonsummer/article/details/136281042

可以看到,生成的“信息窗+主题知识”并非正好凑满1页,因此需要在每个word里添加一些时事内容了。控制字数多少与版面样式(在一页内的2分栏左右)。

第5步:增添文字内容,确定板式,把文字内容重新导入EXCEL,然后批量制作WROD


(1)在19份word里补全文字,控制字数

(2)最终确保 信息窗撑满半页、主题说明撑满半页

6、将以上第一次生成19份word(正确样式)做为“源文件”放入“03修改补充版”的新建文件夹。

复制“04合成新信息窗主题知识”的19份word(修订过的)到“03修改补充版”

# https://blog.csdn.net/lau_jw/article/details/114383781

from openpyxl import load_workbook
from docx import Document
import glob


# 把 ”03去掉回车版“里面修改过的word文件 复制一份到 “03修改补充版”
#  

import os
import shutil
 

path = r'D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识'
# 源文件夹路径
source_folder= path+r'\04合成新信息窗主题知识'
# 目标文件夹路径
target_folder = path+r'\03修改补充版'
 
# 遍历源文件夹中的所有文件
for file in os.listdir(source_folder):
    # 判断文件类型为 Word(后缀名为 .docx、.doc)
    if file.endswith('.docx') or file.endswith('.doc'):
        # 构建源文件的完整路径
        source_file = os.path.join(source_folder, file)
        
        # 将文件从源文件夹复制到目标文件夹
        target_file = os.path.join(target_folder, file)
        shutil.copy2(source_file, target_file)

7、把19份修改过的WORD,写入EXCEL写入(让内容逐步丰富,此时不要删除空格了。)

(1)信息窗内容(读取第3行开始到“中4班”之间的内容,包含回车)

(2)主题说明内容( 还是通过获取“主题名称”“主题说明”“主题目标”“家园共育”的段落数,进行提取。其中家园共育的最大索引是=2)

(3)因为内容、格式、空行都是调整过了,所以不需要对EXCEL的内容删除空行、空格、清除格式。

 (4)重点说明

这份原始模板里面的格式必须每列都改成文本,以免添加进去的文字出现内容缺失

代码展示:

# https://blog.csdn.net/lau_jw/article/details/114383781

from openpyxl import load_workbook
from docx import Document
import glob


# 把 ”03去掉回车版“里面修改过的word文件 复制一份到 “03修改补充版”
#  

import os
import shutil
 
print('-----复制修改过的文件“04合成新信息窗和主题知识”到03修改补充版,类似与再次放在“03去掉回车”-----')

path = r'D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识'
# 源文件夹路径
source_folder= path+r'\04合成新信息窗主题知识'
# 目标文件夹路径
target_folder = path+r'\03修改补充版'
 
# 遍历源文件夹中的所有文件
for file in os.listdir(source_folder):
    # 判断文件类型为 Word(后缀名为 .docx、.doc)
    if file.endswith('.docx') or file.endswith('.doc'):
        # 构建源文件的完整路径
        source_file = os.path.join(source_folder, file)
        
        # 将文件从源文件夹复制到目标文件夹
        target_file = os.path.join(target_folder, file)
        shutil.copy2(source_file, target_file)

print('---读取新的信息窗和主题说明里面的灰色字体部分,但是不要清除空格---')

path = r"D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识"
workbook = load_workbook(path + r'\10 改周次过渡模板_信息窗主题知识.xlsx')
sheet = workbook.active


number = 0
number1 = 0
d=[]

for file in glob.glob(path + r'\04合成新信息窗主题知识\*.docx'):
    print(file)
    doc = Document(file)
    
    
    # 上面是提取信息窗灰色字体内容
    content_lst = []    
    for paragraph in doc.paragraphs[3:]:
        # 改过的模板里面,第0行周次,第1行亲爱的家长, 第2行你们好,从第3行开始选取
        content = paragraph.text
               
         # 判断单元格中的文字是否有空格
        if content == '中4班':
            # 改过的模板里面,碰到“中4”就不读取了            
            break        
           
        else:  
            q = '    ' + content  # 如果段落有文字,读取整段,并在前面加4个空格(2字符)
            content_lst.append(q)
              
    print(content_lst)

    content = '\n'.join(content_lst)
    number += 1
    # 写入AB列
    sheet.cell(row=number+1, column=1).value = number
    sheet.cell(row=number+1, column=2).value = content



    # 下面是提取主题知识4块内容
    

    #获取每个文档的行数  
    print("段落数:"+str(len(doc.paragraphs)))#段落数为13,每个回车隔离一段
    d=len(doc.paragraphs)

    for i in range(len(doc.paragraphs)):  
        if '主题说明:' in doc.paragraphs[i].text:
            h1=i            # 主题说明在第几行
            print('主题说明',h1)
            
    for i in range(len(doc.paragraphs)):          
        if '主题目标:'in doc.paragraphs[i].text:
            h2=i            # 主题说明在第几行
            print('主题目标',h2)
    for i in range(len(doc.paragraphs)):  
        if '家园共育:' in doc.paragraphs[i].text:
            h3=i            # 家园共育在第几行

        # # #家园共育结束值等于总行数
    h4=d

    z1=[]    # 主题名称:
    #  查找“主题名称”所在的行
    for i in range(len(doc.paragraphs)):  
        # print("第"+str(i)+"段的内容是:"+doc.paragraphs[i].text)
        if '主题名称:' in doc.paragraphs[i].text:
            a1=doc.paragraphs[i].text                    
            z1.append(a1[5:])
            # 在同一行上,从5开始提取后面的汉字
        # print(z1)
    content1 = '\n'.join(z1)      # 组合并加回车
    
    z2=[]    # 主题说明:        
    # 提取“主题说明”
    for paragraph2 in doc.paragraphs[h1+1:h2]:        #          主题说明h0固定是1行,所以从2开始提取,主题目标 是提取的行数h1-1, 索引取值,还是h1
        t22 = paragraph2.text    
        t2= '    ' + t22        
        z2.append(t2)
    print(z2)    
    content2 = '\n'.join(z2)      # 组合并加回车

    z3=[]    # 主题目标:        
    # 提取“主题目标”
    for paragraph3 in doc.paragraphs[h2+1:h3]:        #          主题说明h0固定是1行,所以从2开始提取,主题目标 是提取的行数h1-1, 索引取值,还是h1
        t3 = paragraph3.text               
        z3.append(t3)
    print(z3)    
    content3 = '\n'.join(z3)      # 组合并加回车

    z4=[]    # 家园共育:        
    # 提取“家园共育”
    for paragraph4 in doc.paragraphs[h3+1:-2]:        #          主题说明h0固定是1行,所以从2开始提取,主题目标 是提取的行数h1-1, 索引取值,还是h1
        t4 = paragraph4.text               
        z4.append(t4)
    print(z4)    
    content4 = '\n'.join(z4)      # 组合并加回车
 
    number1 += 1
    # sheet.append([number, content1,content2,content3,content4])  # number是序号,一共遍历提取了几分Word的内容,content是主题知识中间部分的内容
    # 写入第9=13列
    sheet.cell(row=number+1, column=9).value = number1
    sheet.cell(row=number+1, column=10).value = content1
    sheet.cell(row=number+1, column=11).value = content2
    sheet.cell(row=number+1, column=12).value = content3
    sheet.cell(row=number+1, column=13).value = content4
 
workbook.save(path + r'\11 中4班下学期_信息窗主题知识.xlsx')
workbook.close()


# print('--打开XLSX-,把里面的空格删除,把1、替换成1.--')# 
# # 打开Excel文件
# workbook = load_workbook(path + r'\11 中4班下学期_信息窗主题知识.xlsx')
# # 获取第一个工作表
# worksheet = workbook.active

# # # 遍历每个单元格
# for row in worksheet.iter_rows():
    
#     for cell in row:        
#         if cell.value and isinstance(cell.value, str):            
#             # 清除单元格内文字的格式
#             cell.value = cell.value.strip()
            

# #         # 判断单元格中的文字是否有空格
# #         if ' ' in str(cell.value):
# #             # 替换空格为无空格
# #             cell.value = str(cell.value).replace(' ', '')

# #         if ' ' in str(cell.value):
# #             # 替换空格为无空格
# #             cell.value = str(cell.value).replace(' ', '')

# #         # 替换文本
# #         for s in range(1,10):
# #             if cell.value and isinstance(cell.value, str):
# #                 cell.value = cell.value.replace("{}、".format(s), "{}.".format(s))

# # 保存修改后的Excel文件
# workbook.save(path + r'\11 中4班下学期_信息窗主题知识.xlsx')

# # 关闭Excel文件
# workbook.close()

 (5)EXCEL还是使用

EXCEL看不出是否提取了所有的内容并格式正确。所以在生成一次19份WROD

8、再次生成19份WORD(板式都在A4半页)

# 一、导入相关模块,设定excel所在文件夹和生成word保存的文件夹
from docxtpl import DocxTemplate
import pandas as pd
import os
import xlwt
import xlrd
import os ,time
import random
from win32com.client import constants,gencache
from win32com.client.gencache import EnsureDispatch
from win32com.client import constants # 导入枚举常数模块
import os,time
import docx
from docx import Document
from docx.shared import Pt 
from docx.shared import RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT



# path=r'D:\\test\\02办公类\\90周计划4份\\06 信息窗+主题知识'+'\\'
path = r"D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识"
print(path)

file_path=path+r'\04合成新信息窗主题知识(修改补充版)'
print(file_path)

# 二、遍历excel,逐个生成word(小标签.docx是前面的模板)
try:
    os.mkdir(file_path)
except:
    pass



list = pd.read_excel(path+'\\11 中4班下学期_信息窗主题知识.xlsx')
# 信息窗
title = list["title"].str.rstrip()
name =list["name"]
content=list["content"]
classroom =list["classroom"].str.rstrip() # str.rstrip()都是文字格式
T1 =list["T1"].str.rstrip() # 没有str.rstrip()是数字格式
T2 =list["T2"].str.rstrip()# 没有str.rstrip()是数字格式
time=list["time"].str.rstrip() 

# 主题知识
titlename = list["titlename"].str.rstrip()
name1=list["name1"]
sm=list["sm"].str.rstrip()# 没有str.rstrip()是数字格式
mb=list["mb"].str.rstrip()# 没有str.rstrip()是数字格式
gy=list["gy"].str.rstrip()# 没有str.rstrip()是数字格式
classroom1 =list["classroom1"].str.rstrip() # str.rstrip()都是文字格式
# T1 =list["T1"].str.rstrip() # 没有str.rstrip()是数字格式
# T2 =list["T2"].str.rstrip()# 没有str.rstrip()是数字格式
time1=list["time1"].str.rstrip() 


# 遍历excel行,逐个生成
num = list.shape[0]
for i in range(num):
    context = {
       "title": title[i],
       "content": content[i],        
       "classroom": classroom[i],
       "name" :name[i],
       "T1": T1[i],
       "T2": T2[i],       
       "time": time[i], 

        "name1": name1[i],
        "titlename": titlename[i],
        "sm": sm[i],  
        "mb" :mb[i],
        "gy" :gy[i],      
        "classroom1": classroom1[i],       
        "time1": time1[i],          
       
    }
  
    tpl = DocxTemplate(path+'\\12 信息窗主题知识_竖版双.docx')
    # tpl = DocxTemplate(path+'\\12 信息窗主题知识_横版双.docx')
 
    tpl.render(context)
    # tpl.save(file_path+r"\\第{}周 {}班 信息窗({}).docx".format('%02d'%name[i],classroom[i],time[i]))
    tpl.save(file_path+r"\\{} 第{}周 信息窗主题知识({})({})({}班下学期)-{}.docx".format('%02d'%name[i],title[i],time[i],time1[i],classroom[i],titlename[i]))


文件名称很长,因为里面包含了两组起止日期(便于后面做png图片时,选择起止时间)

word文件名+后缀docx

png文件名+后缀png

……通过反复的循环操作,让本次信息窗+主题知识的板式、字数、符合标准——两篇在A4一页上,信息窗和主题说明分别在半页上,

到此,本学的信息窗+主题说明完成了!!!!


后续是文本打印纸张和网络上传图片

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值