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

作品展示

背景需求:

本学年墙面贴的是“信息窗+主题名称”合并一页A4横版。

将两份代码合并

【办公类-22-06】周计划系列(1)“信息窗” (2024年调整版本)-CSDN博客文章浏览阅读258次,点赞8次,收藏5次。【办公类-22-06】周计划系列(1)“信息窗” (2024年调整版本)https://blog.csdn.net/reasonsummer/article/details/136270848【办公类-22-07】周计划系列(2)“主题知识” (2024年调整版本)-CSDN博客文章浏览阅读85次,点赞4次,收藏3次。【办公类-22-07】周计划系列(2)“主题知识” (2024年调整版本)https://blog.csdn.net/reasonsummer/article/details/136279321

主要步骤:

1、doc更改,统一文件名长度

2、doc转docx

3、docx去掉回车符(空行)

4、提取word内容导入EXCEL

5、在word模板里,将EXCEL内容填入,保存多个docx

6、把docx转成png

6、把1张png切割成2张png(班级主页)

素材准备:

都是“06 信息窗+主题知识”下的内容

第01步:原有文件的名称 1-9周改成01-09周

原来的主题知识资料(内容包含主题知识和信息窗)

文件名格式不统一,有的周后面有空格,有的窗后面有主题名称……需要统一改成——第XX周 信息窗+主题说明.doc(17)

A:先统一格式

B:在对1-9周的数字进行2位数补全

代码展示:

微调部分——统计文件名的长度:

如:第9周 主题知识.doc   一共12字符,对所有12字符的文件名进行加0处理,如果 第10周 主题知识.doc 等于13 字符,就不要加0

import os
import time


path =r"D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识\01doc"
print('--把文件名称统一 第X周 信息窗---')

fileList=os.listdir(path)
print(fileList)

for file in fileList: 
  # 如果格式不统一 提取所有的周次
  split_str = file.split('窗')
  newname1 = split_str[0]  # _的第0部分=序号 
  print(newname1)
  newname2= split_str[1]  # _的第0部分=序号 
  print(newname2)
  newname=newname1+'窗+主题知识.doc'
  oldname_path = os.path.join(path,file)
  # 文件新路径
  newname_path = os.path.join(path,newname)
  # 新旧对调
  os.rename(oldname_path, newname_path)

# 延时
time.sleep(2)

print('--把第1周改成第01周 ---')

fileList=os.listdir(path)
print(fileList)

for file in fileList:
  if len(file)==16:    # 第9周 信息窗+主题说明.doc   16是一位数的周次 
    print(file)   
    split_str = file.split('第')
    newname1 = split_str[0]  # _的第0部分=序号 
    print(newname1)
    newname2= split_str[1]  # _的第0部分=序号 
    print(newname2)
    newname=newname1+'第0'+newname2
    oldname_path = os.path.join(path,file)
    # 文件新路径
    newname_path = os.path.join(path,newname)
    # 新旧对调
    os.rename(oldname_path, newname_path)
  if len(file)==17:     # 第10周 信息窗+主题说明.doc  17是两位数的周次    
    pass
  

  

终端显示

第2步:原有文件格式从doc变成docx

代码展示——doc文件下的19个doc转到docx文件下的19个docx

import os
from win32com import client as wc
import time
#  注意:目录的格式必须写成双反斜杠
path=r"D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识"
oldpath=path+r'\01doc'  # 使用绝对地址(可更改)原文件doc地址
newpath=path+r'\02docx'# 使用绝对地址(可更改)docx地址

# 提取所有doc内的19周doc周计划的路径
files=[]
for file in os.listdir(oldpath):
    # 找出文件中以.doc结尾并且不以~$开头的文件(~$是为了排除临时文件)
    if file.endswith('.doc') and not file.startswith('~$'): 
        files.append(oldpath+'\\'+file)
        print(files)

# 打开doc文件下的doc文件,另存到docx文件下的docx文件
for file in files:
    word = wc.Dispatch("Word.Application")
    print("已处理文件:"+file)
#     # 打开文件
    doc = word.Documents.Open(file)
    # # 将文件另存为到docx文件夹,另存为.docx
    new=newpath+'\\'+file[-17:]+'x'
    print(new)
    doc.SaveAs("{}".format(new), 12)    # 12表示docx格式
    doc.Close()

           
 
 

第3步:删除docx里面的回车符

每份docx里面会有一些空行回车

删除空行回车符

代码展示

from docx import Document
from openpyxl import load_workbook
import glob

import os

path=r"D:\test\02办公类\91周计划4份_2024年中4班\06 信息窗+主题知识"
for file in glob.glob(path + r'\02docx\*.docx'):   # 读取所有以前的信息窗参考资料
    doc = Document(file)
    for paragraph in doc.paragraphs:  # 读取文档段落
        if len(paragraph.text) == 0:
            p = paragraph._element
            p.getparent().remove(p)
            p._p = p._element = None
       
    doc.save(path+r'\03去掉回车\\'+file[-18:])
# # ————————————————
# # 版权声明:本文为CSDN博主「lsjweiyi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
# # 原文链接:https://blog.csdn.net/lsjweiyi/article/details/121728630

第4步:整理“主题知识”的EXCEL信息,写入 中4班下学期“主题知识”.xlsx

信息窗和主题知识的{{}}做在一份上,部分内容名称相同,需要修改名字

两份做在一份上,需要改很多的字母

代码展示:

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

from openpyxl import load_workbook
from docx import Document
import glob

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

number = 0
number1 = 0

for file in glob.glob(path + r'\03去掉回车\*.docx'):
    print(file)
    doc = Document(file)
    
    # 上面是提取信息窗灰色字体内容
    content_lst = []    
    for paragraph in doc.paragraphs[1:]:
        content = paragraph.text
         # 判断单元格中的文字是否有空格
        if content == '中八班':
            break
        else:
            q = '    ' + content
            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)
            # z2.append(i)
            # print(z2)
    for i in range(len(doc.paragraphs)):          
        if '主题目标:'in doc.paragraphs[i].text:
            h2=i            # 主题说明在第几行
            # print('主题目标',h2)
            # z3.append(i)
            # print(z3)
    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
        t2 = paragraph2.text               
        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:]:        #          主题说明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()

思路解析——提取“信息窗+主题知识”中灰色部分的内容

重点说明:

0、提取“家长”到”中八班”之间灰色汉字

1、读取关键词所在的行数

2、提取段落内容

3、在Excel里面去除格式和空格(不用手工批量删除了!)

结果展示

主题说明并非一周一篇,因此需要选择主题(几周)的时间范围

第5步:读取word模板,把EXCEL-主题知识 的内容 合成19份主题知识 Docx

代码展示

# 一、导入相关模块,设定excel所在文件夹和生成word保存的文件夹
from docxtpl import DocxTemplate
import pandas as pd
import os
import xlwt
import xlrd
import os
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"].str.rstrip()# 没有str.rstrip()是数字格式
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],classroom[i],time[i]))


   

运行结果

接下来就是每周打开补充添加一些时事内容了。

只需要添加文字,控制字数多少与版面样式,不需要手码日期了(*^_^*)

本篇是第一步提取原来的信息窗+主题知识,后续再19份word里补全文字,控制字数后,还需要将第一次生成19份word做为“源文件”放入“去掉空格”文件夹内,进行第二次的写入EXCEL写入(让内容逐步丰富,此时不要删除空格了,)然后,再次生成19份WORD……通过反复的循环操作,让本次信息窗+主题知识的板式、字数、符合标准。

以上是第1步:提取“信息窗主题说明”的原始信息(以前的内容需要改内容)

以下是第2步:对修改过的内容呢的文件(添补文字、增加回车,让版面统一),做EXCEL提取,再次获取本年度的信息窗周计划,以便能够在遇到格式调整时,继续快速批量制作。

【办公类-22-07】周计划系列(3-2)“信息窗+主题知识(优化)” (2024年调整版本)-CSDN博客【办公类-22-07】周计划系列(3-2)“信息窗+主题知识(优化)” (2024年调整版本)https://blog.csdn.net/reasonsummer/article/details/136354870?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22136354870%22%2C%22source%22%3A%22reasonsummer%22%7D


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿夏reasonsummer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值