话说现在的老师太偷懒了,给出的试卷质量参差不齐,有的喜欢用A3,有的喜欢用8K
手动操作的思路是,直接打开word,将页面布局的分栏改成1栏,再将纸张大小改成A4,最后将纸张方向改成纵向
不支持双面打印的打印机,需要自己先打奇数页,再翻过来打偶数页
问题是9门功课,一门最少七八张试卷,这得干到啥时候?
代码的思路是,直接转成pdf再从中间切割,然后左右两边可能会有一点点留白,不影响使用
目前测试8K,A3能正常分割
库安装:
pip install docx2pdf
pip install PyMuPDF
代码:
[Python] 纯文本查看 复制代码import os
import fitz # PyMuPDF
from docx2pdf import convert
def list_docx_files(folder_path):
"""
枚举指定文件夹下的所有.docx文件
参数:
folder_path (str): 要搜索的文件夹路径,例如 r'c:\文件'
返回:
list: {file_name:文件名(不含后缀),file_folder:文件路径}
"""
docx_files = []
try:
# 检查路径是否存在
if not os.path.exists(folder_path):
print(f"错误:路径不存在")
return docx_files
# 遍历目录树
for root, dirs, files in os.walk(folder_path):
for file in files:
# 检查文件扩展名是否为.docx(不区分大小写)
if file.lower().endswith('.docx'):
full_path = os.path.join(root, file)
dict_files = {}
dict_files['file_folder'] = full_path
filename_without_ext = os.path.splitext(file)[0]
dict_files['file_name'] = filename_without_ext
docx_files.append(dict_files)
except Exception as e:
print(f"发生错误: {e}")
return docx_files
def docx_to_pdf(input_docx, output_pdf):
convert(input_docx, output_pdf)
def a3_to_a4(pdf_path, output_path):
# 打开PDF文件
pdf = fitz.open(pdf_path)
new_pdf = fitz.open()
for page_num in range(len(pdf)):
page = pdf[page_num]
rect = page.rect # 获取当前页面的矩形区域
# 二选一,如果是左右分割,则删除 split_type = 'horizon' 这句
split_type = 'horizon'
split_type = 'vertical'
if split_type == 'vertical':
# 垂直裁切:将页面分为左右两半
width = rect.width
# 创建左半部分
left_rect = fitz.Rect(rect.x0, rect.y0, rect.x0 + width/2, rect.y1)
# 创建右半部分
right_rect = fitz.Rect(rect.x0 + width/2, rect.y0, rect.x1, rect.y1)
# 将左右两部分分别添加为新页面
for part_rect in [left_rect, right_rect]:
new_page = new_pdf.new_page(width=part_rect.width, height=part_rect.height)
new_page.show_pdf_page(new_page.rect, pdf, page_num, clip=part_rect)
elif split_type == 'horizon':
# 水平裁切:将页面分为上下两半
height = rect.height
# 创建上半部分
top_rect = fitz.Rect(rect.x0, rect.y0, rect.x1, rect.y0 + height/2)
# 创建下半部分
bottom_rect = fitz.Rect(rect.x0, rect.y0 + height/2, rect.x1, rect.y1)
# 将上下两部分分别添加为新页面
for part_rect in [top_rect, bottom_rect]:
new_page = new_pdf.new_page(width=part_rect.width, height=part_rect.height)
new_page.show_pdf_page(new_page.rect, pdf, page_num, clip=part_rect)
new_pdf.save(output_path)
new_pdf.close()
pdf.close()
if __name__ == "__main__":
# 输入DOCX文件夹路径,由用户给出
input_docx = r"C:\Users\administrator\Desktop\暑假作业\暑假作业学生版数学"
# 临时存放DOCX转PDF文件的路径,无需修改,除非你需要自定义该文件夹名
temp_pdf = input_docx + '\original_pdf'
if not os.path.exists(temp_pdf):
os.makedirs(temp_pdf)
# 输出转成A4尺寸的PDF文件的路径,无需修改,除非你需要自定义该文件夹名
output_pdf = input_docx + '\output_A4_pdf'
if not os.path.exists(output_pdf):
os.makedirs(output_pdf)
# 枚举路径下所有的docx文档
docx_list = list_docx_files(input_docx)
# 将word文档转pdf
for docx in docx_list:
temp_pdf_file = temp_pdf + '\\' + docx["file_name"] + '.pdf'
docx_to_pdf(docx['file_folder'],temp_pdf_file)
# 将原始的A3大小的pdf转成A4大小,转换后的文件均已A4_开头,并存放在output_A4_pdf目录下
out_pdf_file = output_pdf + '\\A4_' + docx["file_name"] + '.pdf'
a3_to_a4(temp_pdf_file, out_pdf_file)