简体   繁体   English

将 Excel 文件导出为 PDF 格式而不丢失超链接功能

[英]Export Excel file to PDF format without loosing hyperlink functionality

I use the following code to read in an Excel file and export select sheets to a PDF:我使用以下代码读取 Excel 文件并将 select 工作表导出到 PDF:

import win32com.client

xlsx_path = "" #insert path to excel file
pdf_path = "" #insert path to pdf output

excel = win32com.client.gencache.EnsureDispatch("Excel.Application")
wb = excel.Workbooks.Open(xlsx_path)
sheet_list = ["Sheet1", "Sheet2"]
wb.Worksheets(sheet_list).Select()
wb.ActiveSheet.ExportAsFixedFormat(0, pdf_path)
wb.Close()

Within the Excel file I have hyperlinks that toggle between the sheets, but the links loose their functionality in PDF format.在 Excel 文件中,我有在工作表之间切换的超链接,但这些链接失去了 PDF 格式的功能。 An example of the macro I use is:我使用的宏的一个例子是:

=HYPERLINK("[file.xlsx]Sheet1!A1","back")

My question is if there is any way to preserve the links functionality in PDF form?我的问题是是否有任何方法可以保留 PDF 形式的链接功能?

This isn't exactly something simple and formatting might be an issue, but if what you have in your Excel file are simple tables with text and numbers (without images or fancy formatting), there might be a way using LaTeX:这不是一件简单的事情,格式可能是个问题,但如果您在 Excel 文件中拥有的是带有文本和数字的简单表格(没有图像或花哨的格式),则可能有一种使用 LaTeX 的方法:

  1. Export the Excel content in LaTeX format.以 LaTeX 格式导出 Excel 内容。 You can find already existing packages for VBA ( like this one ) or for Python ( like this one )that can do that or at least be a good starting point.您可以找到已经存在的 VBA(像这个)或 Python(像这个)的包,它们可以做到这一点,或者至少是一个很好的起点。
  2. In the LaTeX file, do a find and replace to change your links to the right format for LaTeX and add the section info.在 LaTeX 文件中,进行查找和替换以将链接更改为 LaTeX 的正确格式并添加部分信息。 (See this question for more details). (有关更多详细信息,请参阅此问题)。
  3. Compile the LaTeX file to a PDF via VBA (using a shell command) or Python. Compile the LaTeX file to a PDF via VBA (using a shell command) or Python.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM