Is it possible to convert a excel file to PDF using UiPath

excel
uiautomation
robot
activities
studio

#1

Hi,

I’m new to Uipath and I have a task to copy the data from excel file into PDF. I have an excel file which contains data in table and image. Is it possible to convert excel file to PDF?. If yes, could anyone help me on this?

Thanks.


#2

Hi @Dhanushree

Check the below post

Thanks
Ashwin S


#3

you can try this also,


#4

@ddrdushy1

I want to convert using Uipath. Is it possible to convert excel file to PDF which also contain image?

Thank you.


#5

use that powershell script to do the conversion. it will work for all kind of excel files. but you need to ammend few lines in the script.
try invoke powershell activity to trigger the scripts.

or use this utility to do the conversion. trigger through the command prompt.


#6

I’m trying to use this in a workflow, but am not sure how to utilize the ‘Invoke power shell’ activity with UiPath. It works fine when I save it as a .ps1 file and execute with powershell, but I tried putting it into UiPath and it didn’t work.

I tried by putting the entire script into the CommandText input, made the TypeArgument System.Management.Automation.PSObject, changed all double quotes " to single quotes ’ and made sure the box IsScript was checked. Sometimes it runs fine with no errors. However, it will sometimes run the script and output the new PDF file as expected, but UiPath will throw an error with the following message: "Unable to cast object of type ‘System.String’ to type ‘System.Management.Automation.PSObject’. Exception Type: InvalidCastException.

Any ideas what portion of the script is throwing that error & how to get it fixed?

EDIT: I changed the TypeArgument to String as I was passing in two string arguments. This erased the PowerShellVariables I had already set up so I put them in again and it now seems to be working. My question is, why is it required & is changing the TypeArgument the correct thing to do in this instance? What would happen if my arguments I needed to pass to the power shell script were of multiple types?

EDIT 2: I just realized that it doesn’t work when trying to pass an argument, but does work when passing a variable. When passing an argument it gets an exception that the PDF file couldn’t be found. Not sure why it is throwing that error as it is supposed to be creating a new file, so of course it isn’t found! Any ideas? And anyone know why it works fine when passing a variable, but not an argument, even if they have the exact same value?

I attached a workflow - can someone else give it a try?
ExcelToPDF.xaml (8.0 KB)


How to Get names of Excel worksheets
#7

You need to swap your argument values.

image


#8

Thanks, it’s so simple I thought I was going crazy! Whoops :blush:


#9

Hi,
I am new to UIPath and I saw that these codes are interesting to convert excel to pdf

"$xlFixedFormat = ‘Microsoft.Office.Interop.Excel.xlFixedFormatType’ -as [type]
$objExcel = New-Object -ComObject excel.application
$objExcel.visible = $false

$workbook = $objExcel.workbooks.open($ExcelFilePath, 3)
$workbook.Saved = $true
$workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $PDFFilePath)
$objExcel.Workbooks.close()

$objExcel.Quit()"

need help in explaining what each line of codes mean.

Thanks so much


#10

This is a powershell (PS) script that is executed by UiPath. I’ll do my best to explain what each line is doing. The overall goal is to create a PDF file that is a replica of an entire excel workbook.

  1. $xlFixedFormat = ‘Microsoft.Office.Interop.Excel.xlFixedFormatType’ -as [type]

This line defines the PS variable $xlFixedFormat as an excel FixedFormatType. This is the built-in method in excel to convert to PDF. See more info here: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/workbook-exportasfixedformat-method-excel?f=255&MSPPError=-2147217396

  1. $objExcel = New-Object -ComObject excel.application

This line creates creates a new object (excel)

  1. $objExcel.visible = $false

This line allows it to work in the background as I don’t want people interacting with it accidentally.

  1. $workbook = $objExcel.workbooks.open($ExcelFilePath, 3)

Using the previously defined $objExcel, it uses the Open method with 2 arguments: $ExcelFilePath and 3.

  1. $workbook.Saved = $true

This saves the workbook you’ve opened. I included it because linked data being updated means the workbook may have changed. If you don’t update links, feel free to leave this out.

  1. $workbook.ExportAsFixedFormat($xlFixedFormat::xlTypePDF, $PDFFilePath)

This is where the actual conversion takes place. As mentioned before, excel has a built-in way to convert to PDF called ExportAsFixedFormat. The arguments supplied are:

  • $xlFixedFormat::xlTypePDF
    This specifies to convert to PDF (only other option is XPS)

  • $PDFFilePath
    This is the full filepath of where you’d like the newly created PDF file to be saved. Be sure the extension is included. This needs to be an input PowerShellVariable in UiPath.

  1. $objExcel.Workbooks.close()

This closes the workbook you’re working with.

  1. $objExcel.Quit()

This quits the excel application