需求如下:word文件670份,每份8-17页不等,要求奇数页打-5至-1页(两张),偶数页的文件打后4页(两张)。最终查到解决方案如下:
(1)word文件的docx格式先转为pdf格式,防止在分页时出现错误;
(2)pdf格式文件按奇偶数页不同分为两大类,放入不同文件夹;
(3)使用截取页数,偶数页截后4页,奇数页截倒数5至1页;
(4)使用批量pdf打印即可。
代码如下:
1 docx文件转pdf
# Imports =============================================================
import comtypes.client
import time
import os
file_path = r"C:/Users/Administrator/Desktop/E/"
out_path = r"C:/Users/Administrator/Desktop/B/"
# Variables and Inputs=================================================
# File = r'C:\Users\Administrator\Desktop\TS-PM-0001矮地茶饮片※生产工艺规程(B00版).docx'
# Or .doc, rtf files
# Functions ============================================================
def convert_word_to_pdf(inputFile,outputFile):
''' the following lines that are commented out are items that others shared with me they used when
running loops to stop some exceptions and errors, but I have not had to use them yet (knock on wood) '''
word = comtypes.client.CreateObject('Word.Application')
# word.visible = False
# time.sleep(3)
doc = word.Documents.Open(inputFile)
doc.SaveAs(outputFile, FileFormat = 17)
doc.close()
word.visible = False
word.Quit()
# Main Body=================================================================
def main():
for name in os.listdir(file_path):
print(name)
file = file_path + name
outfile = out_path + name[:-5] +'.pdf'
if file.split(".")[-1] == 'docx':
convert_word_to_pdf(file,outfile)
print("^"*30)
if __name__ == '__main__':
main()
2pdf文件按奇偶分类
import os
import PyPDF2
import shutil
file_path = r"C:/Users/Administrator/Desktop/B/"
ou_file_path = r"C:/Users/Administrator/Desktop/oushu/"
ji_file_path=r"C:/Users/Administrator/Desktop/jishu/"
for name in os.listdir(file_path):
print(name)
file = file_path + name
ou_file=ou_file_path + name
ji_file=ji_file_path + name
if file.split(".")[-1] == 'pdf':
reader = PyPDF2.PdfFileReader(file)
# 获取单个文件页数
pageNum = reader.getNumPages()
if pageNum%2 == 0:
shutil.move(file,ou_file)
else:
shutil.move(file,ji_file)
# print(pageNum)
3 分别截取需要的4页
# -- coding: utf-8 --
# 导入PYPDF2库
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
ou_file_path = r"C:/Users/Administrator/Desktop/oushu/"
# ji_file_path=r"C:/Users/Administrator/Desktop/jishu/"
pdf_file_path=r"C:/Users/Administrator/Desktop/pdf2/"
def main():
for name in os.listdir(ou_file_path):
print(name)
ou_file=ou_file_path + name
pdf_file=pdf_file_path + name
# start = -5
# 切分结束页面
# end = -1
start = -4
# 切分结束页面
end = 0
split_single_pdf(ou_file, start, end, pdf_file)
def split_single_pdf(read_file, start_page, end_page, pdf_file):
# 1. 获取原始pdf文件
fp_read_file = open(read_file, 'rb')
# 2. 将要分割的PDF内容格式化
pdf_input = PdfFileReader(fp_read_file)
# 3. 实例一个 PDF文件编写器
pdf_output = PdfFileWriter()
# 4. 把3到4页放到PDF文件编写器
for i in range(start_page, end_page):
pdf_output.addPage(pdf_input.getPage(i))
# 5. PDF文件输出
with open(pdf_file, 'wb') as pdf_out:
pdf_output.write(pdf_out)
print(f'{read_file}分割{start_page}页-{end_page}页完成,保存为{pdf_file}!')
if __name__ == '__main__':
main()
4 批量打印
import win32api
import win32print
import os
import time
def printer_loading(filename):
win32api.ShellExecute (
0,
"print",
filename,
'/d:"%s"' % win32print.GetDefaultPrinter (),
".",
0
)
time.sleep(20)
path=r'C:\Users\Administrator\Desktop\pdf'
for filenames in os.listdir(path):
file_path=os.path.join(path,filenames)
print("您要打印的文件是%s"%filenames)
printer_loading(os.path.join(path,filenames))
参考了网上较多高手的代码,在此深感谢意。