[英]How to get a range of cells while retaining their format from an .xlsx document?
I am trying to print a range of cells from xlsx to pdf using below code我正在尝试使用以下代码打印从 xlsx 到 pdf 的一系列单元格
from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\\Users\....\\2020.xlsx')
ws = books.Worksheets['TEST']
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\\Users\.....\\trial.pdf')
This code works fine.此代码工作正常。 However, I would like to tweak to print only the cells ranging from
A1
to K50
;但是,我想调整以仅打印从
A1
到K50
的单元格; and should retain the cell formatting from excel when converted to PDF.并且在转换为 PDF 时应保留 excel 的单元格格式。 Is this possible?
这可能吗?
It is very hard to format using python code with other third party packages.使用 python 代码和其他第三方包很难格式化。 Below solution is useful when either the formatting requires a lot of effort or when the formatting may be dynamic & you may need to just print the excel like the way it is:
当格式化需要大量工作或格式化可能是动态的并且您可能需要像这样打印 excel 时,以下解决方案很有用:
This solution is done using the below code (note the trick is using IgnorePrintAreas=False
), however, the ranges that I wanted to print I set it up as the printable range on excel.这个解决方案是使用下面的代码完成的(注意技巧是使用
IgnorePrintAreas=False
),但是,我想打印的范围我将它设置为 excel 上的可打印范围。 This will print the range that I am interested in & with the format that is present in Excel.这将使用 Excel 中存在的格式打印我感兴趣的范围。
from win32com import client
xlApp = client.Dispatch("Excel.Application")
books = xlApp.Workbooks.Open('C:\\Users\....\\2020.xlsx')
ws = books.Worksheets['TEST']
ws.Visible = 1
ws.ExportAsFixedFormat(0, 'C:\\Users\.....\\trial.pdf',IgnorePrintAreas=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.