简体   繁体   English

Python-使用现有格式保存新的Excel工作表

[英]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.

相关问题 如何使用 Pandas 在现有 excel 文件中保存新工作表? - How to save a new sheet in an existing excel file, using Pandas? 使用 Python:过滤 Excel 工作表并将过滤后的视图另存为新工作表 - Using Python: Filter an Excel Sheet and Save Filtered View as New Sheet 在 Python 中格式化 Excel 表 - Format an Excel Sheet in Python "使用 python pandas 将现有的 excel 表附加到新的数据框" - Append existing excel sheet with new dataframe using python pandas Python Pandas:在Excel中获取特定行并保存到新表中 - Python pandas: Get specific row in an excel and save to a new sheet 我想将特定列保存到 python 中的新 excel 表中 - i want to save a particular column into a new excel sheet in python 将DataFrame保存到现有Excel工作表的下一行 - Save DataFrame to next row in existing excel sheet Python:如何将 dataframe 导出到现有的 Excel 工作表但不创建新工作表 - Python: how can I export dataframe to existing Excel sheet but not creating a new sheet 使用python在现有Excel文件中的不同工作表中的相同Excel中的新工作表中合并结果摘要 - Consolidating Result Summary in new sheet in same excel from different sheets on a existing excel file using python 如何使用Python合并来自不同工作簿的多个同名excel工作表并保存到新的excel工作簿中 - How to merge multiple excel sheet with the same name from different workbooks and save into a new excel workbook using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM