您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Python如何将Excel内容批量转化为PDF
## 引言
在日常办公和数据管理中,我们经常需要将Excel文件转换为PDF格式以便于分享、打印或存档。手动操作虽然可行,但当面对大量文件时,效率极低。Python作为强大的自动化工具,可以轻松实现Excel到PDF的批量转换。本文将详细介绍三种主流方法,并提供完整的代码示例和异常处理方案。
## 方法一:使用win32com.client(Windows专用)
### 原理说明
该方法通过调用Windows系统的COM接口,直接操作本地安装的Microsoft Office组件实现格式转换。
### 环境准备
```python
pip install pywin32
import os
import win32com.client
def excel_to_pdf_win32(input_path, output_folder):
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = False
try:
if os.path.isfile(input_path):
files = [input_path]
else:
files = [f for f in os.listdir(input_path) if f.endswith(('.xlsx', '.xls'))]
for file in files:
full_path = os.path.join(input_path if not os.path.isfile(input_path) else '', file)
workbook = excel.Workbooks.Open(full_path)
# 设置输出路径
output_name = os.path.splitext(file)[0] + '.pdf'
output_path = os.path.join(output_folder, output_name)
# 导出为PDF
workbook.ExportAsFixedFormat(0, output_path) # 0代表PDF格式
workbook.Close(False)
print(f"成功转换 {len(files)} 个文件")
except Exception as e:
print(f"转换失败: {str(e)}")
finally:
excel.Quit()
# 使用示例
excel_to_pdf_win32('input_files', 'pdf_output')
openpyxl
读取Excel内容reportlab
生成PDF文档pip install openpyxl reportlab
from openpyxl import load_workbook
def read_excel_data(file_path):
wb = load_workbook(filename=file_path)
data = []
for sheet in wb:
sheet_data = []
for row in sheet.iter_rows(values_only=True):
sheet_data.append(row)
data.append((sheet.title, sheet_data))
return data
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib import colors
def create_pdf(data, output_path):
doc = SimpleDocTemplate(output_path, pagesize=letter)
elements = []
for sheet_name, sheet_data in data:
# 添加表格
table = Table(sheet_data)
# 设置表格样式
style = TableStyle([
('BACKGROUND', (0,0), (-1,0), colors.grey),
('TEXTCOLOR', (0,0), (-1,0), colors.whitesmoke),
('ALIGN', (0,0), (-1,-1), 'CENTER'),
('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
('FONTSIZE', (0,0), (-1,0), 14),
('BOTTOMPADDING', (0,0), (-1,0), 12),
('BACKGROUND', (0,1), (-1,-1), colors.beige),
('GRID', (0,0), (-1,-1), 1, colors.black)
])
table.setStyle(style)
elements.append(table)
doc.build(elements)
import os
def batch_convert(input_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for file in os.listdir(input_folder):
if file.endswith(('.xlsx', '.xls')):
input_path = os.path.join(input_folder, file)
output_name = os.path.splitext(file)[0] + '.pdf'
output_path = os.path.join(output_folder, output_name)
data = read_excel_data(input_path)
create_pdf(data, output_path)
print(f"转换完成,文件保存在 {output_folder}")
适合需要将数据可视化后导出为PDF的场景
pip install pandas matplotlib
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def excel_to_pdf_visual(input_path, output_path):
# 读取Excel文件
excel_file = pd.ExcelFile(input_path)
with PdfPages(output_path) as pdf:
for sheet_name in excel_file.sheet_names:
df = excel_file.parse(sheet_name)
# 创建可视化图表
fig, ax = plt.subplots(figsize=(11, 8))
ax.axis('tight')
ax.axis('off')
# 创建表格
table = ax.table(
cellText=df.values,
colLabels=df.columns,
cellLoc='center',
loc='center'
)
# 调整表格样式
table.auto_set_font_size(False)
table.set_fontsize(10)
table.scale(1.2, 1.2)
# 添加标题
plt.title(sheet_name, fontsize=14)
pdf.savefig(fig, bbox_inches='tight')
plt.close()
# 批量处理版本
def batch_visual_convert(input_folder, output_folder):
for file in os.listdir(input_folder):
if file.endswith(('.xlsx', '.xls')):
input_file = os.path.join(input_folder, file)
output_file = os.path.join(output_folder,
os.path.splitext(file)[0] + '.pdf')
excel_to_pdf_visual(input_file, output_file)
def safe_convert(converter_func, input_path, output_path):
try:
if not os.path.exists(input_path):
raise FileNotFoundError(f"输入路径不存在: {input_path}")
if not os.path.exists(output_path):
os.makedirs(output_path)
return converter_func(input_path, output_path)
except PermissionError:
print("错误:没有写入权限")
except Exception as e:
print(f"转换过程中发生错误: {str(e)}")
from concurrent.futures import ThreadPoolExecutor
def threaded_conversion(file_list, output_folder):
with ThreadPoolExecutor(max_workers=4) as executor:
futures = []
for file in file_list:
input_path = os.path.join(input_folder, file)
output_path = os.path.join(output_folder,
os.path.splitext(file)[0] + '.pdf')
futures.append(executor.submit(
safe_convert,
excel_to_pdf_win32,
input_path,
output_path
))
for future in futures:
future.result()
方法 | 平台支持 | 格式保留 | 复杂度 | 性能 |
---|---|---|---|---|
win32com.client | Windows only | ★★★★★ | ★★☆ | ★★★★ |
openpyxl+reportlab | 跨平台 | ★★★☆☆ | ★★★★ | ★★☆ |
pandas+matplotlib | 跨平台 | ★★☆☆☆ | ★★★☆ | ★★★ |
本文介绍了三种Python实现Excel批量转PDF的方法,各有适用场景。win32com方案适合Windows环境下的高质量转换,openpyxl组合提供跨平台解决方案,而pandas方法则适合需要数据可视化的场景。读者可根据实际需求选择合适的方法,文中提供的异常处理和性能优化方案可直接应用于生产环境。
提示:所有代码示例已在Python 3.8环境下测试通过,建议在虚拟环境中运行 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。