[英]Python - save new Excel sheet with existing format
I want to save excel sheet with existing sheet formats and background color. 我想用现有的工作表格式和背景颜色保存Excel工作表。
I could save successfully to a new file with out old file format. 我可以成功保存到没有旧文件格式的新文件。
How can I keep the existing excel sheet format when save in to a new file. 保存到新文件时,如何保持现有的Excel工作表格式。
writer = pd.ExcelWriter('New.xlsx', engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='DIFF', index=False)
writer.save()
I had no good experience with pandas and format styles... what works if you use win32com like this example: 我对熊猫和格式样式没有很好的经验。。。如果您使用win32com这样的示例,那怎么办?
from win32com.client import DispatchEx
excel = DispatchEx('Excel.Application')
wbP=excel.Workbooks.Open(r'C:\Temp\Junk\Temp.xlsx')
wbG=excel.Workbooks.Open(r'C:\Temp\Junk\Temp2.xlsx')
wbG.Worksheets('TAA2').Copy(Before=wbP.Worksheets("TAA"))
wbP.SaveAs(r'C:\Temp\Junk\Temp.xlsx')
excel.Quit()
del excel # ensure Excel process ends
this copies everything... (styles, formats, formulas etc.) 这会复制所有内容...(样式,格式,公式等)
Another option is copy the whole workbook and delete the not needed sheets, if you create a new file and not editing an existing one... Copy worksheet from one workbook to another one using Openpyxl 另一种选择是,如果创建新文件而不编辑现有文件,则复制整个工作簿并删除不需要的工作表 ... 使用Openpyxl将工作表从一个工作簿复制到另一个工作簿
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.