在excel文件中打印select的工作表为python中的pdf

我需要编写一个python脚本来读取excel文件,find每个工作表,然后用excel中定义的标准格式打印这些文件。

我发现以下问题如何在Python中打开Excel文件? 这指向我http://www.python-excel.org/

这使我能够find每个工作表的名称。

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文件令人困惑

格式化function不包含在xlrd 0.6.1版中:杂项表级和书籍级项目,例如打印版面,屏幕窗格

格式化

介绍

xlrd版本0.6.1中新增的这些function旨在提供(1)在屏幕或PDF文件中显示/呈现电子表格内容(比如说)所需的信息

请参阅https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966

这是真的吗? 可以使用其他一些包打印到PDF?

对于unix我看到有什么用于Windows的http://dag.wieers.com/home-made/unoconv/ ? 我发现https://gist.github.com/mprihoda/2891437,但不知道如何使用它。

这似乎是放置这个答案的地方。

最简单的forms是:

 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

你也可以使用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)