简体   繁体   English

Python xlwings 复制粘贴格式

[英]Python xlwings copy paste with format

Apologies for no coding provided, this is really a generic question.抱歉没有提供编码,这真的是一个通用的问题。

I'm using Python xlwings library, and trying to copy a sheet from one workbook to another new workbook, then hard-code the sheet in the newly created workbook.我正在使用 Python xlwings 库,并尝试将工作表从一个工作簿复制到另一个新工作簿,然后在新创建的工作簿中对该工作表进行硬编码。 Effectively same as "Copy / Paste Values and source formatting".与“复制/粘贴值和源格式”有效相同。

I wasn't able to find any documentation on this, and thank you in advance for your help!我找不到任何关于此的文档,在此先感谢您的帮助!

edit: someone mentioned that I should include an example.编辑:有人提到我应该包括一个例子。 Here it is but it's kind hard to show the format in an Excel file.在这里,但很难在 Excel 文件中显示格式。 the following code will copy/paste "sht" into a new workbook but the "new_sht" will contain formulas.以下代码将“sht”复制/粘贴到新工作簿中,但“new_sht”将包含公式。 I'm trying to hard-code all the values while preserving the number format (eg. with thousands separator, percentage sign, etc)我正在尝试对所有值进行硬编码,同时保留数字格式(例如,使用千位分隔符、百分号等)

import xlwings as xw
wb = xw.Book('example1.xlsx')
sht = wb.sheets['sheet1']
new_wb = xw.Book()
new_sht = new_wb.sheets[0]
sht.api.Copy(Before = new_sht.api)

Answering my own question as I just figured out what I wanted to accomplish.回答我自己的问题,因为我刚刚弄清楚我想要完成什么。 The following code will hardcode the values while preserve the formatting, since it's essentially pasting value-only to an already formatted area.以下代码将在保留格式的同时对值进行硬编码,因为它本质上只是将值粘贴到已格式化的区域。

new_sht.range('A1:C10').value = new_sht.range('A1:C10').value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM