gpt4 book ai didi

python - 在 python 中将 excel 文件中选定的工作表打印为 pdf

转载 作者:太空狗 更新时间:2023-10-29 20:11:15 25 4
gpt4 key购买 nike

我需要编写一个 python 脚本来读取 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 文档说的很困惑

Formatting features not included in xlrd version 0.6.1: Miscellaneous sheet-level and book-level items e.g. printing layout, screen panes

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

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

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

对于 unix,我看到有 http://dag.wieers.com/home-made/unoconv/有什么适合 window 的吗?我找到了 https://gist.github.com/mprihoda/2891437但还不知道如何使用它。

最佳答案

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

最简单的形式:

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

关于python - 在 python 中将 excel 文件中选定的工作表打印为 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16683376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com