2013-05-22 79 views
5

我需要編寫一個python腳本來讀取excel文件,找到每個工作表,然後用excel中定義的標準格式打印這些文件。在excel文件中打印選擇的工作表爲pdf格式

我發現以下問題How can I open an Excel file in Python?它向我指出http://www.python-excel.org/

這讓我找到每個工作表的名稱的能力。

import xlrd 
book = xlrd.open_workbook("myfile.xls") 
print "Worksheet name(s):", book.sheet_names() 

這導致

Worksheet name(s): [u'Form 5', u'Form 3', u'988172 Adams Road', u'379562 Adams Road', u'32380 Adams Road', u'676422 Alderman Road', u'819631 Appleyard Road', u'280998 Appleyard Road', u'781656 Atkinson Road', u'949461 Barretts Lagoon Road', u'735284 Bilyana Road', u'674784 Bilyana Road', u'490894 Blackman Road', u'721026 Blackman Road'] 

現在我要打印的以數字爲PDF啓動每個工作表。

,所以我可以

worksheetList=book.sheet_names() 
for worksheet in worksheetList: 
if worksheet.find('Form')!=0: #this just leaves out worksheets with the word 'form' in it 
    <function to print to pdf> book.sheet_by_name(worksheet) #what can I use for this? 

或類似於上面的東西......我可以用實現這一目標?

的XLRD文檔是混淆它說

格式設置功能,不包括在xlrd版本0.6.1:其他 板級和圖書一級的項目如印刷版面,屏幕窗格

格式化

介紹

的特點,在xlrd版本0.6.1新的這個系列,旨在 提供所需的信息(1)在屏幕上或在PDF文件中顯示/呈現電子表格 內容(說)

請參閱https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966

這是真的嗎?可以使用其他一些軟件包打印到pdf嗎?

對於unix,我發現有http://dag.wieers.com/home-made/unoconv/任何東西的Windows?我發現https://gist.github.com/mprihoda/2891437,但無法弄清楚如何使用它。

+1

嘿@GeorgeC,您是否找到了解決方案?我正在尋找一種方法來「打印」整個xsl到pdf,所以如果你提交你的解決方案作爲anwer將是有益的:) –

+0

@GustavoVargas我沒有使用以下,因爲它不保留格式化,但xtopdf似乎做一個好的解決方案 - dancingbison.com/products.html,開發人員也非常有幫助。 – GeorgeC

回答

3

這似乎是放置這個答案的地方。

在最簡單的形式:

import win32com.client 

o = win32com.client.Dispatch("Excel.Application") 

o.Visible = False 

wb_path = r'c:\user\desktop\sample.xls' 

wb = o.Workbooks.Open(wb_path) 



ws_index_list = [1,4,5] #say you want to print these sheets 

path_to_pdf = r'C:\user\desktop\sample.pdf' 



wb.WorkSheets(ws_index_list).Select() 

wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf) 

包括縮放以適應單個頁面,並設置打印區域有點格式化法寶:

import win32com.client 

o = win32com.client.Dispatch("Excel.Application") 

o.Visible = False 

wb_path = r'c:\user\desktop\sample.xls' 

wb = o.Workbooks.Open(wb_path) 



ws_index_list = [1,4,5] #say you want to print these sheets 

path_to_pdf = r'C:\user\desktop\sample.pdf' 

print_area = 'A1:G50' 



for index in ws_index_list: 

    #off-by-one so the user can start numbering the worksheets at 1 

    ws = wb.Worksheets[index - 1] 

    ws.PageSetup.Zoom = False 

    ws.PageSetup.FitToPagesTall = 1 

    ws.PageSetup.FitToPagesWide = 1 

    ws.PageSetup.PrintArea = print_area 



wb.WorkSheets(ws_index_list).Select() 

wb.ActiveSheet.ExportAsFixedFormat(0, path_to_pdf) 

我還通過啓動模塊github如果你想看看:https://github.com/spottedzebra/excel/blob/master/excel_to_pdf.py

+0

只需添加所有其他PageSetup選項:https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.pagesetup_properties.aspx –

+0

如果選項卡具有多個打印區域,那麼如何定義? – Gavin

+0

我不確定,但我有關於如何繼續的建議。 我想你可以添加多個「print_area」變量。所以,你可以做到以下幾點: print_area = A1:G50 print_area2 = A52:G52 您可能還需要通過片材指數環兩次或做的代碼頁設置位兩次for循環。 這些只是想法。我實際上沒有嘗試打印多個打印區域。 – spottedzebra

0

你也可以使用https://www.coolutils.com/TotalExcelConverterXPython

示例

import win32com.client 
import os.path 

c = win32com.client.Dispatch("ExcelConverter.ExcelConverterX") 

src="C:\\test\\test.xlsx"; 
dest="C:\\test\\test.pdf"; 

c.convert(src, dest, "-c PDF -log c:\\test\\Excel.log"); 

if not os.path.exists(file_path): 
    print(c.ErrorMessage)