I have been searching many ways to print an excel file without opening Excel app, I used Microsoft.Office.Interop.Excel in C# and it works pretty well so I decided to look for a way to make it works in python too, I found IronPython but I need just python, then I found pythonnet to make .NET assemblies work in python.
The problem is that intalling it (or trying to) from the source https://github.com/pythonnet/pythonnet It gives me an error about Windows SDK not found or something.
Then I installed it by pip and the installation was successful, but when I try to import or to add a reference another error is shown:
Unable to find assembly 'Microsoft.Office.Interop.Excel'.
at Python.Runtime.CLRModule.AddReference(String name)
I have downloaded and installed Interop Assemblies from here so they are supposed to be installed.
I found a way to print excel files in here using IronPython and the Interop dll.
My main problem is that I need to print some excel files without opening Excel application, if you have another option is welcome
If there is no other option, what should I do to make it find the assemblies?
Additional Info: To print in C# I used this:
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
String CompletePath = path;
Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(CompletePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];
try
{
// Open the Workbook:
// Get the first worksheet.
// (Excel uses base 1 indexing, not base 0.)
ws.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
ws.PageSetup.FitToPagesTall = 1;
ws.PageSetup.FitToPagesWide = 1;
ws.PageSetup.TopMargin = 0;
ws.PageSetup.HeaderMargin = 0;
ws.PageSetup.RightMargin = 0;
ws.PageSetup.LeftMargin = 0;
ws.PageSetup.BottomMargin = 0;
ws.PageSetup.FooterMargin = 0;
ws.PageSetup.CenterVertically = true;
ws.PageSetup.CenterHorizontally = true;
//ws.PageSetup.Zoom = false;
ws.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing, PrinterName, Type.Missing, Type.Missing, Type.Missing);
return true;
}
catch (Exception ex)
{
LogSystem.TextLog.create();
LogSystem.TextLog.Write("ERROR ", LogSystem.ErrorType.Error, DateTime.Now, ex.Message);
return false;
}
finally
{
// Cleanup:
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(ws);
wb.Close(false, Type.Missing, Type.Missing);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wb);
excelApp.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp);
}