在这个信息爆炸的时代,我们经常需要从各种来源获取和整理信息,其中,Word和PDF文档是一种常见且重要的信息载体,但是,如何批量转换Word文档格式?如何批量合并PDF文档?如何从PDF中批量提取所需信息并将其导入Excel,却是一个让人头疼的问题。
今天,我们就来探讨一种简单高效的方法,让你轻松实现Word批量格式转换、PDF文档批量合并、PDF批量信息提取并保存到Excel中,常规的手动方法处理不仅操作费时费力,而且,这种方法没有复用性,本节使用Python代码解决以上的所有问题,实现办公自动化。
1.Word文档批量转PDF格式
如下共计有15个Word合同文档,每一个Word文档均有不同的合同内容,现在需要将所有的Word文件格式转化为PDF文件格式,如果通过“另存为”的方式来修改PDF格式,不容易实现,使用Python仅需几行代码,即可批量将Word格式转化为PDF格式。

首先使用convert_word_to_pdf函数接受一个目录路径作为参数,然后遍历该目录下的所有文件,对以.docx结尾的文件,使用win32com.client模块打开Word应用程序,将其保存为PDF文件,并使用FileFormat=17指定PDF文件格式,最后,关闭Word文档和应用程序。
并且,该代码指定源目录和目标目录的路径,如果没有最终保存文件的目录,可以使用os.makedirs()新建一个文件目录,将转化格式后的PDF文件保存在新的文件夹中。
import osimport win32com.clientfrom docx import Documentdef convert_word_to_pdf(source_directory, target_directory): word_app = win32com.client.Dispatch("Word.Application") word_app.Visible = False # 创建目标文件夹 os.makedirs(target_directory, exist_ok=True) for filename in os.listdir(source_directory): if filename.endswith(".docx"): docx_file = os.path.join(source_directory, filename) pdf_file = os.path.splitext(filename)[0] + ".pdf" pdf_path = os.path.join(target_directory, pdf_file) doc = word_app.Documents.Open(docx_file) doc.SaveAs(pdf_path, FileFormat=17) doc.Close() word_app.Quit()# 指定源目录和目标目录的路径source_directory = r"D:\系统桌面(勿删)\Desktop\合同文件"target_directory = r"D:\系统桌面(勿删)\Desktop\PDF文件转化"# 调用函数并传入源目录和目标目录的路径convert_word_to_pdf(source_directory, target_directory)
如下即为批量格式转换后的文件,15个Word文档均转化为PDF格式。

2.批量合并PDF文档为一个
将上面的15个PDF文档合并为1个PDF文档,使用os.getcwd()获取当前目录的路径作为源目录的路径,创建一个名为merged_ pdfs函数,传入导入数据路径和导出数据路径,循环遍历.pdf文件,使用append函数批量合并并输出。
import osfrom PyPDF2 import PdfMergerdef merge_pdfs(source_directory, output_file): merger = PdfMerger() for filename in os.listdir(source_directory): if filename.endswith(".pdf"): pdf_path = os.path.join(source_directory, filename) merger.append(pdf_path) merger.write(output_file) merger.close()# 获取当前目录路径current_directory = os.getcwd()# 指定源目录和输出文件的路径source_directory = r"D:\系统桌面(勿删)\Desktop\PDF文件转化"output_file = os.path.join(current_directory, r"D:\系统桌面(勿删)\Desktop\PDF文件合并.pdf")# 调用函数并传入源目录和输出文件的路径merge_pdfs(source_directory, output_file)
如下缩略页将15个PDF文档合并为1个PDF文档。

3.批量提取PDF信息存为Excel
PDF文档合并完以后,需要提取合同中的“合同编号、甲方、乙方、品名、采购数量、采购单价、总价”这几个字段数据, 并将其保存为Excel文档,使用Python几行代码即可搞定。

首先,使用PyPDF2打开PDF文档,然后,循环遍历合同中的每一页信息,将遍历出来的信息打印出来,可以看到具体的字段信息内容。
import re import pandas as pd import PyPDF2 # 打开PDF文件 with open(r'D:\系统桌面(勿删)\Desktop\PDF文件合并.pdf', 'rb') as file: reader = PyPDF2.PdfFileReader(file) num_pages = reader.numPages # 通过每一页提取信息 info = [] for page_num in range(num_pages): page = reader.getPage(page_num) text = page.extractText() print(text)
接下来使用正则表达式,将需要提取的字段信息数据使用append函数合并起来,合并起来的数据为一个字典类型数据,pd.DataFrame() 可以将字典数据保存为二维数据,df.to_excel导出为Excel形式的数据。
import re import pandas as pd import PyPDF2 # 打开PDF文件 with open(r'D:\系统桌面(勿删)\Desktop\PDF文件合并.pdf', 'rb') as file: reader = PyPDF2.PdfFileReader(file) num_pages = reader.numPages # 通过每一页提取信息 info = [] for page_num in range(num_pages): page = reader.getPage(page_num) text = page.extractText() # 使用正则表达式匹配所需信息 HT_No = re.findall(r'合同编号:\s*(.*)', text) name1 = re.findall(r'甲方:\s*(.*)', text) name2 = re.findall(r'乙方:\s*(.*)', text) catege = re.findall(r'品名:\s*(.*)', text) weight = re.findall(r'采购数量(斤):\s*(.*)', text) price = re.findall(r'采购单价(元 /斤):\s*(.*)', text) price_sum = re.findall(r'总价(元):\s*(.*)', text) # 将信息添加到列表中 info.append({'合同编号': HT_No[0] if HT_No else '', '甲方': name1[0] if name1 else '', '乙方': name2[0] if name2 else '', '品名': catege[0] if catege else '', '采购数量': weight[0] if weight else '', '采购单价': price[0] if price else '', '总价': price_sum[0] if price_sum else ''} ) # 将信息保存为Excel文件 df = pd.DataFrame(info) df.to_excel(r'D:\系统桌面(勿删)\Desktop\数据提取.xlsx', index=False) 
以上,我们使用Python成功解决Word批量转PDF格式、批量合并PDF文档、批量提取PDF内信息到Excel,使用Python轻松实现,不仅省时省力,而且可复用性强,只需部分修改即可应用其他文档操作,这对于信息获取、整理和分析具有重要的意义,如果你在代码编译过程中有任何疑问或建议,可在评论区留言,随时与我交流。