在excel文件中打印选定的工作表,以便在python中使用pdf [英] Print chosen worksheets in excel files to pdf in python

查看:1015
本文介绍了在excel文件中打印选定的工作表,以便在python中使用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.

我发现以下问题如何在Python中打开Excel文件?,它指向我 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


格式化

Formatting

简介

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/ 任何窗口?我发现 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:\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)

如果你想看,我也在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

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

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