在python中将excel文件中选择的工作表打印为pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16683376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print chosen worksheets in excel files to pdf in python
提问by GeorgeC
I need to write a python script to read excel files, find each worksheet and then print these to pdf with the standard formating defined in the excel.
我需要编写一个python脚本来读取excel文件,找到每个工作表,然后使用excel中定义的标准格式将它们打印为pdf。
I found the following question How can I open an Excel file in Python?which pointed me to http://www.python-excel.org/
我发现以下问题如何在 Python 中打开 Excel 文件?这将我指向了http://www.python-excel.org/
This gives me the ability to find the names of each worksheet.
这使我能够找到每个工作表的名称。
import xlrd
book = xlrd.open_workbook("myfile.xls")
print "Worksheet name(s):", book.sheet_names()
This results in
这导致
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']
Now I want to print each worksheet which starts with a number to a pdf.
现在我想将每个以数字开头的工作表打印为 pdf。
So I can
所以我可以
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?
or something similar to above...what can I use to achieve this?
或类似于上面的东西......我可以用什么来实现这一目标?
The XLRD documentation is confusing it says
XLRD 文档令人困惑,它说
Formatting features not included in xlrd version 0.6.1: Miscellaneous sheet-level and book-level items e.g. printing layout, screen panes
xlrd 0.6.1 版中未包含的格式功能:其他图纸级和书籍级项目,例如打印布局、屏幕窗格
and
和
Formatting
Introduction
This collection of features, new in xlrd version 0.6.1, is intended to provide the information needed to (1) display/render spreadsheet contents (say) on a screen or in a PDF file
格式化
介绍
此功能集合是 xlrd 0.6.1 版中的新增功能,旨在提供 (1) 在屏幕或 PDF 文件中显示/渲染电子表格内容所需的信息
see https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966
见https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966
Which is true? can some other package be used to print to pdf?
哪个是真的?可以使用其他一些包打印到 pdf 吗?
For unix I see that there is http://dag.wieers.com/home-made/unoconv/anything for windows? I found https://gist.github.com/mprihoda/2891437but can't figure out how to use it yet.
对于 unix,我看到http://dag.wieers.com/home-made/unoconv/ 有什么适用于 Windows 的?我找到了https://gist.github.com/mprihoda/2891437但不知道如何使用它。
采纳答案by spottedzebra
This seems like the place to put this answer.
这似乎是放置这个答案的地方。
In the simplest form:
以最简单的形式:
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)
Including a little formatting magic that scales to fit to a single page and sets the print area:
包括一些格式化魔法,可以缩放以适应单个页面并设置打印区域:
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)
I have also started a module over a github if you want to look at that: https://github.com/spottedzebra/excel/blob/master/excel_to_pdf.py
如果您想查看该模块,我还通过 github 启动了一个模块:https: //github.com/spottedzebra/excel/blob/master/excel_to_pdf.py

