Clade
Clade

Reputation: 986

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:

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. 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?

Upvotes: 2

Views: 360

Answers (1)

DecimalTurn
DecimalTurn

Reputation: 4129

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:

  1. Export the Excel content in LaTeX format. 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.
  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. (See this question for more details).
  3. Compile the LaTeX file to a PDF via VBA (using a shell command) or Python.

Upvotes: 1

Related Questions