小工具知识网

当前位置: 首页 > 行业知识 > 【办公类-22-15】周计划系列(5-6)“周计划-06 周计划打印pdf(docx删除内容转PDF)“ (2024年调整版本)

行业知识

【办公类-22-15】周计划系列(5-6)“周计划-06 周计划打印pdf(docx删除内容转PDF)“ (2024年调整版本)

2026-12-02 23:09:34 来源:互联网转载

作品展示

背景需求:

前期用docx(删除第一页反思部分内容)转PDF转png(第一页)的方式获得上传网页用的图片。

【办公���-22-14】周计划系列(5-5)“周计划-05 上传周计划png(docx转PDF转png)“ (2024年调整版本)-CSDN博客文章浏览阅读600次,点赞11次,收藏9次。【办公类-22-14】周计划系列(5-5)“周计划-05 上传周计划png(docx转PDF转png)“ (2024年调整版本)https://blog.csdn.net/reasonsummer/article/details/136660866

去年我用转成了png图片打印的周计划,感觉打印出来的图片会比直接用docx打印的内容模糊一点、字体小一点。

所以今年我想试试 在docx内将第一页反思删除、第2-5页内容删除。然后将docx转成PDF,再合并,打印出来看看这种docx转成PDF直接转的方法下,文字是否会更清晰一点。

素材准备:

第一张表的反思部分不要,第2-4页内容删光

代码展示:

'''周计划第一页的合并打印(docx删除第1页反思,删除第2-4页所有内容,转为PDF,19份PDF合并打印PDF)作者:阿夏:时间:2024年3月18日'''# 19个docx合并成一个PDF,便于打印import osfrom docx2pdf import convertfrom PyPDF2 import PdfMergerfrom docx import Documentpath=r'D:\test办公类周计划4份_2024年中4班 周计划'old=path+r'加粗测试'new=path+r'docx合并pdf'new_ls=new+r'\零时文件夹' # 检查文件夹是否存在if not os.path.exists(new_ls):    # 如果文件夹不存在,则新建文件夹    os.makedirs(new_ls)# 获取docx文件列表docx_files = os.listdir(old)docx_files = [f for f in docx_files if f.lower().endswith('.docx')]docx_files = docx_files[:]  # 只处理前10个docx文件# 处理.docx文件import timefrom docx import Documentfrom docx.shared import Ptfrom docx.enum.section import WD_ORIENTfrom docx.shared import Cmprint('--------1、遍历把周计划docx删掉反思内容,另存到jpg上传文件夹---------')folder_path = oldfor file_name in os.listdir(folder_path):    print(file_name)    if file_name.endswith('.docx'):        file_path = os.path.join(folder_path, file_name)        doc = Document(file_path)        # 删除第0张表格里反思格子里的内容        table = doc.tables[0]  # 假设第一页只有一个表格        cell = table.cell(1, 8)  # 获取第1行第8列的单元格        cell.text = ''  # 清空单元格内容     # 找到第一个分节符(分页符)后的位置        start_index = 3        # 第1、2行 第3行内容保留(都是第一页上的段落文字        for i, paragraph in enumerate(doc.paragraphs):            if paragraph.runs:                if paragraph.runs[0].text == '\x0c':  # 分页符的Unicode码为'\x0c'                    start_index = i + 1                    break        # 删除第一个分节符后的段落,标题日期,这是教案表格还在的        for i in range(start_index, len(doc.paragraphs)):            for run in doc.paragraphs[i].runs:                run.text = ''        # 删除教案表格2-5。删除后会有4个空行        tables = doc.tables[1:]        # 表格0是周计划,1-3是教案表格,需要删除        for table in tables:            table._element.getparent().remove(table._element)        # 删除表格后,会有4个段落空行,把空行删除        for paragraph in doc.paragraphs:            if not paragraph.text.strip():  # 如果段落是空行                # 删除空行                p = paragraph._element                p.getparent().remove(p)        # 这是只有一页周计划了,但是显示是竖版的,需要改成横版        section = doc.sections[0]        # 设置页面方向为横版        section.orientation = WD_ORIENT.LANDSCAPE        # 设置页面的宽度和高度        section.page_width = Cm(29.7)  # 设置页面宽度,842磅为A4纸宽度        section.page_height =Cm(21.0)  # 设置页面高度,595磅为A4纸高度        # 设置页面边距为上下左右各1厘米        section.top_margin = Cm(1)        section.bottom_margin = Cm(1)        section.left_margin = Cm(1)        section.right_margin = Cm(1)        # 保存新的Word文档        doc.save(new_ls+r'\{}'.format(file_name))        time.sleep(1)# # 将零时文件夹docx文件转换为PDFpdf_files = []for ls_name in os.listdir(new_ls):    print(ls_name)    if ls_name.endswith('.docx'):#         docx_path = os.path.join(new_ls, ls_name)        pdf_file = ls_name[:-5] + '.pdf'        pdf_path = os.path.join(new_ls, pdf_file)        convert(docx_path, pdf_path)        time.sleep(1)        pdf_files.append(pdf_path)# 合并PDF文件merger = PdfMerger()for pdf_file in pdf_files:    merger.append(pdf_file)# 保存合并后的PDF文件output_file = os.path.join(new, '(打印)2024年2月周计划合并版.pdf')merger.write(output_file)merger.close()print('合并完毕,结果保存在{}'.format(output_file))import shutil# 删除临时文件夹    shutil.rmtree(new_ls)

作品展示:

一共19周的周计划第一页

上一篇:云服务器配置怎么选的?

下一篇:云友下载器使用秘籍,一键掌握高效下载技巧!