帮我写一个脚本,将指定文件夹内的PDF文件转换成表格文件,表格运行时选择指定文件,
时间: 2024-11-29 10:18:32 浏览: 28
你可以使用Python的`tabula-py`库来读取PDF文件,并结合`pandas`库将其转换为CSV或其他表格格式。以下是一个基本的脚本示例,假设你已经安装了`tabula-py`和`pandas`库:
```python
import os
import tabula
import pandas as pd
# 指定需要转换的PDF文件夹路径
folder_path = "path_to_your_pdf_folder"
# 获取文件夹内所有的PDF文件
pdf_files = [os.path.join(folder_path, file) for file in os.listdir(folder_path) if file.endswith('.pdf')]
for pdf_file in pdf_files:
# 将PDF转换为DataFrame
df = tabula.read_pdf(pdf_file)
# 给每个PDF生成一个单独的CSV文件,例如使用当前时间作为文件名
timestamp = pd.Timestamp.now().strftime('%Y%m%d%H%M%S')
output_file = f"output_{timestamp}.csv"
output_path = os.path.join(folder_path, output_file)
# 写入CSV文件
df.to_csv(output_path, index=False)
print("转换完成。")
阅读全文
相关推荐


















