Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

配合 poexcel 中 excel2pdf 方法的修改 #111

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions demo/poexcel/Excel转PDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,44 @@

import office

# 假设Excel共有3个工作表,表名分别为 sheet1 sheet2 sheet3

# 下列的路径应该是到目录而非文件.excel_path到文件不影响使用.pdf_path到文件实际会多一层目录
# 例如下面的示例,最终生成的pdf文件路径为 D:\test\程序员晚枫.pdf\程序员晚枫.pdf

# 转换整个工作簿
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf")

# 仅转换第一个工作表 方法1
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
include=1)

# 仅转换第一个工作表 方法2
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
exclude=[2, 3])

# 仅转换第一个工作表 方法3
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
exclude=["sheet2", "sheet3"] # 这里也可以是 [ 2 , "sheet3"]
)

# 仅转换第一和第三个工作表 方法1
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
include=[1, 3])

# 仅转换第一和第三个工作表 方法2
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
include=["sheet1", "sheet3"] # 这里也可以是 [ 1 , "sheet3"]
)

# 仅转换第一和第三个工作表 方法3
office.excel.excel2pdf(excel_path=r"D:\test\程序员晚枫.xlsx",
pdf_path=r"D:\test\程序员晚枫.pdf",
exclude=2
)
10 changes: 8 additions & 2 deletions office/api/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@ def split_excel_by_column(filepath: str, column: int, worksheet_name: str = None


@instruction
def excel2pdf(excel_path, pdf_path):
def excel2pdf(excel_path, pdf_path, include: int or str or [int] or [str] or None = None,
exclude: int or str or [int] or [str] or None = None):
"""
https://blog.csdn.net/qq_57187936/article/details/125605967

include: 要包括的工作表,可以为工作表名(str),工作表索引号(int,从1开始),或由上述两者(可以混用)组成的列表,默认为None,当为None时,不生效.
exclude: 要排除的工作表,可以为工作表名(str),工作表索引号(int,从1开始),或由上述两者(可以混用)组成的列表,默认为None,当为None时,不生效.
详见官方文档: https://docs.xlwings.org/en/latest/api/book.html#xlwings.Book.to_pdf

"""
poexcel.excel2pdf(excel_path, pdf_path)
poexcel.excel2pdf(excel_path, pdf_path, include, exclude)

@instruction
def merge2excel(excel_path, output='merge2excel.xlsx'):
Expand Down