用python生产一段把‘F:\\study\\公众号\\使用文件\\2’中所有xlsx表格文件变成pdf文件的代码
时间: 2023-10-04 12:12:11 浏览: 40
import os
import glob
import pandas as pd
from fpdf import FPDF
def convert_xlsx_to_pdf(file_path):
# 读取xlsx文件
excel_data = pd.read_excel(file_path)
# 创建PDF文档
pdf = FPDF()
# 将每个表格存储为一页PDF
for sheet_name, df in excel_data.items():
pdf.add_page()
pdf.set_font("Arial", size=12)
# 将表格数据逐行添加到PDF中
for index, row in df.iterrows():
row_data = " | ".join([str(val) for val in row.values])
pdf.cell(0, 10, txt=row_data, ln=True)
# 保存PDF文件
pdf_output_path = os.path.splitext(file_path) + ".pdf"
pdf.output(pdf_output_path)
# 获取目录下所有xlsx文件的路径
file_list = glob.glob("F:\\study\\公众号\\使用文件\\2\\*.xlsx")
# 转换每个xlsx文件为pdf
for file_path in file_list:
convert_xlsx_to_pdf(file_path)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)