python 多张pdf格式的数据表转换成excel 源代码
时间: 2023-07-27 08:02:01 浏览: 146
import pandas as pd
import tabula
def convert_pdf_to_excel(pdf_files):
for file in pdf_files:
try:
# 使用tabula库将pdf转为DataFrame
df = tabula.read_pdf(file, pages='all')
# 将DataFrame转为Excel并保存
df.to_excel(file.replace('.pdf', '.xlsx'), index=False)
except Exception as e:
print(f"转换文件 {file} 时发生错误:{e}")
# 定义要转换的多个pdf文件路径
pdf_files = ["file1.pdf", "file2.pdf", "file3.pdf"]
# 调用函数进行转换
convert_pdf_to_excel(pdf_files)
上述代码使用了Python中常用的pandas库和tabula库来进行多个PDF数据表转换成Excel的操作。首先定义一个convert_pdf_to_excel函数,该函数用于将多个pdf文件转换为Excel。该函数接受一个包含多个pdf文件路径的列表作为参数。在函数内部,使用了for循环来遍历每个pdf文件,使用tabula库的read_pdf函数将每个pdf文件转换为DataFrame对象。然后,使用pandas库的to_excel函数将DataFrame对象保存为Excel文件,并使用replace函数将文件后缀名从.pdf替换为.xlsx。若遇到转换错误,则会将错误信息打印出来。最后,调用convert_pdf_to_excel函数并传入pdf文件列表来转换所有pdf文件为Excel文件。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)