简体   繁体   English

如何从 an.xlsx 文档中获取一系列单元格,同时保留其格式?

[英]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 ;但是,我想调整以仅打印从A1K50的单元格; 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.

相关问题 如何使用 xlrd 从 XLSX 单元格获取每个字符格式? - How to get each char format from XLSX cell with xlrd? 如何使用python中的xlsx包格式化excel中的特定单元格 - how to format specific cells in excel using xlsx package in python 如何使用 xlsxwriter 在 python 中格式化一系列单元格 - How to format a range of cells in python using xlsxwriter 如何在抓取数据并将其保存为 xlsx 格式时处理特殊字符? - How to handle Special characters while scraping data and saving it in xlsx format? 如何获得过滤后的数据框进行计算,同时将原始数据框保留在熊猫中? - How to get the filtered dataframe for calculations while retaining the original one in pandas? 如何从Python中的xlsx文件获取信息? - How to get information from an xlsx file in Python? 如何在保留格式的同时使用 python 将 excel 文件翻译成另一种语言 - How to translate a excel file to another language using python while retaining the format Selenium - 如何从元素中获取文本但保留子元素源 - Selenium - How to get the text from an element but retaining child element source 编写一个Python函数从输入文件夹中获取excel文件并检查它是csv格式还是xlsx格式 - Write a Python function to get the excel files from the input folder and also check whether it is in csv format or xlsx format pandas + xlsx:根据另一个数据框格式化单元格 - pandas+xlsx: format cells based on another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM