簡體   English   中英

使用Python中的Microsoft.Office.Interop.Excel和CLR打印Excel文件?

[英]Print Excel files with Microsoft.Office.Interop.Excel and CLR in Python?

我一直在尋找許多無需打開Excel應用程序即可打印excel文件的方法,我在C#中使用了Microsoft.Office.Interop.Excel,並且效果很好,所以我決定尋找一種使其也可以在python中工作的方法IronPython,但是我只需要python,然后我發現pythonnet可以使.NET程序集在python中工作。

問題是從源https://github.com/pythonnet/pythonnet安裝 (或嘗試安裝)它給我一個關於Windows SDK找不到或類似問題的錯誤。

然后我通過pip安裝了它,安裝成功,但是當我嘗試導入或添加引用時,顯示另一個錯誤:

 Unable to find assembly 'Microsoft.Office.Interop.Excel'.
   at Python.Runtime.CLRModule.AddReference(String name)

我已經從這里下載並安裝了Interop組件,因此應該將其安裝。

我在這里找到了一種使用IronPython和Interop dll打印excel文件的方法。

我的主要問題是我需要在不打開Excel應用程序的情況下打印一些excel文件 ,如果您有其他選擇,歡迎

如果沒有其他選擇,我應該怎么做才能找到程序集?


附加信息:要在C#中進行打印,我使用了以下方法:

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);
}

雖然我相信有一種方法可以使用win32com.client使Excel自動化( 有一個很好的示例 ),但將.net包裝com互操作代碼並使用Python.Net公開它可能很有用。 考慮可能是.Net更好地支持和簡化了com互操作,但是您應該考慮到Python.Net本身並不是完美的,並且多年沒有積極開發。

接下來是方法:

  1. 使用.net編寫代碼,並使用盡可能簡單的界面公開此代碼
    避免公開COM類型!
    避免管理狀態!
  2. 使用python中的庫。

第1步

  • 創建一個名為ExcelToolsLibrary的新.Net ClassLibrary項目
  • 添加對Microsoft.Office.Interop.Excel的引用
  • 將下一個代碼放在此處:

     namespace ExcelTools { using System; using Microsoft.Office.Interop.Excel; public class ExcelPrinter { public bool PrintFile(string fileName, string printerName) { var excel = new Application(); var workbook = excel.Workbooks.Open(fileName); var worksheet = (Worksheet)workbook.Worksheets[1]; try { SetupPage(worksheet); worksheet.PrintOut(ActivePrinter: printerName ?? Type.Missing); workbook.Close(false); return true; } catch (Exception ex) { LogError(ex); return false; } finally { DisposeExcelObjects(excel,worksheet,workbook); } } private void SetupPage(Worksheet worksheet) { worksheet.PageSetup.Orientation = XlPageOrientation.xlLandscape; //put additional page setup here } private void LogError(Exception e) { //add your logging } private void DisposeExcelObjects(Application excelApp,params object[] comObjects) { try { foreach (var obj in comObjects) System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj); excelApp.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(excelApp); } catch (Exception e) { LogError(e); } } } } 

第2步

要在python中使用此庫:

  • 生成項目並將ExcelToolsLibrary.dll從輸出放置到路徑中可用的任何位置(您可以使用.pth文件 )。
  • 導入庫並像這樣使用它:

     import clr clr.AddReference("ExcelToolsLibrary") # 'ExcelToolsLibrary' is a dll name without the path import ExcelTools # 'ExcelTools' is a .Net namespace printer = ExcelTools.ExcelPrinter() printer.PrintFile(fileName,None) # None means that will be printed on default printer 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM