在python中将excel文件中选择的工作表打印为pdf [英] Print chosen worksheets in excel files to pdf in python

查看:44
本文介绍了在python中将excel文件中选择的工作表打印为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 python 脚本来读取 excel 文件,找到每个工作表,然后使用 excel 中定义的标准格式将它们打印为 pdf.

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.

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

I found the following question How can I open an Excel file in Python? which pointed me to 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()

结果是

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.

Now I want to print each worksheet which starts with a number to a 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?

或者类似于上面的东西......我可以用什么来实现这一目标?

or something similar to above...what can I use to achieve this?

XLRD 文档令人困惑

The XLRD documentation is confusing it says

xlrd 0.6.1 版中未包含的格式化功能:杂项工作表级和书籍级项目,例如打印布局,屏幕窗格

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 版中的新功能集合旨在提供 (1) 显示/渲染电子表格所需的信息屏幕或 PDF 文件中的内容(例如)

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

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

哪个是真的?可以用其他包打印成pdf吗?

Which is true? can some other package be used to print to pdf?

对于 unix,我看到有 http://dag.wieers.com/home-made/unoconv/ 有什么适用于 Windows 的吗?我找到了 https://gist.github.com/mprihoda/2891437 但不知道怎么用呢.

For unix I see that there is http://dag.wieers.com/home-made/unoconv/ anything for windows? I found https://gist.github.com/mprihoda/2891437 but can't figure out how to use it yet.

推荐答案

这似乎是放置此答案的地方.

This seems like the place to put this answer.

最简单的形式:

import win32com.client

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

o.Visible = False

wb_path = r'c:userdesktopsample.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:userdesktopsample.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:userdesktopsample.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:userdesktopsample.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

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

这篇关于在python中将excel文件中选择的工作表打印为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆