简体   繁体   English

如何使用 python 将一个 excel 工作表模板复制到多个 excel 文件中

[英]How to copy one excel sheet template into multiple excel files using python

i am trying to copy an excel spreadsheet which has some text and numbers and a logo image into multiple excel files as a new sheet with source formatting using python, any help is greatly appreciated.我正在尝试将包含一些文本和数字以及徽标图像的 excel 电子表格复制到多个 excel 文件中,作为使用 python 的源格式的新工作表,非常感谢任何帮助。

It's not clear what your end requirement and restrictions are.目前尚不清楚您的最终要求和限制是什么。 The basic requirement;基本要求; "I am trying to copy an excel spreadsheet which has some text and numbers and a logo image into multiple excel files" just indicates you want multiple copies of the same Excel file, the File Management app of your OS can do this, with the limitation perhaps being the resultant naming of each file. “我正在尝试将包含一些文本和数字以及徽标图像的 excel 电子表格复制到多个 excel 文件中”只是表示您想要多个相同 Excel 文件的副本也许是每个文件的最终命名。 If this is your requirement then perhaps something like Python - making copies of a file may help to create the files with the necessary naming.如果这是您的要求,那么可能类似于Python - 制作文件副本可能有助于创建具有必要命名的文件。

If its a workbook with multiple sheets and you only want one sheet copied to new workbooks then openpyxl can help.如果它是一个包含多张工作表的工作簿,并且您只想将一张工作表复制到新工作簿中,那么 openpyxl 可以提供帮助。 Copying sheets is easy enough however the formatting requires extra code.复制工作表很容易,但是格式化需要额外的代码。 If there is just a couple of sheets in the original it may be easier to just remove those sheets before saving your copy, see example code below.如果原始文件中只有几张纸,则在保存副本之前删除这些纸可能会更容易,请参见下面的示例代码。 Otherwise this link may help How to copy worksheet from one workbook to another one using openpyxl?否则,此链接可能有助于如何使用 openpyxl 将工作表从一个工作簿复制到另一个工作簿?

Example: The following code shows how to open an existing workbook, 'templateA.xlsx' which has two sheets, 'Sheet1 & 'Sheet2'.示例:以下代码显示如何打开现有工作簿“templateA.xlsx”,该工作簿有两个工作表“Sheet1 和“Sheet2”。 You only want 'Sheet1' saved as multiple copies (10) named 'template1.xlsx', 'template2.xlsx', 'template3.xlsx'... The code open the original workook, deletes the sheet called 'Sheet2' before making 10 copies.您只想将“Sheet1”保存为多个副本(10),名为“template1.xlsx”、“template2.xlsx”、“template3.xlsx”......代码打开原始工作簿,在制作之前删除名为“Sheet2”的工作表10 份。

from openpyxl import load_workbook

# Load the original Excel workbook
wb = load_workbook("templateA.xlsx")

# delete the Sheet2 that is not required
del wb["Sheet2"]

# Save 10 copies of the workbook with Sheet1 only
for i in range(10):
    wb.save("template" + str(i) + ".xlsx")

The first link python-making-copies-of-a-file can help if your required naming is more complex than the example given.如果您所需的命名比给出的示例更复杂,第一个链接 python-making-copies-of-a-file 会有所帮助。

暂无
暂无

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

相关问题 如何读取多个Excel文件并将其加载到一张Excel工作表中 - How to read multiple excel files and load into one excel sheet Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files - Python: How to copy Excel worksheet from multiple Excel files to one Excel file that contains all the worksheets from other Excel files 如何使用 Python 将数据从一个 Excel 工作表复制到同一工作簿的另一工作表? - How to copy data from One Excel sheet to another sheet for same Workbook Using Python? 如何使用 python 读取多个 excel 表 - How to read multiple excel sheet using python 使用Python(和DataNitro)将单元格从一个Excel工作簿中的特定工作表复制到另一个Excel工作簿中的特定工作表 - Using Python (and DataNitro) to copy cells from a particular sheet in one Excel workbook, to a particular sheet in another Excel workbook 如何使用 Python 从多个文本文件中提取数据到 Excel? (每张纸一个文件的数据) - How do I extract data from multiple text files to Excel using Python? (One file's data per sheet) 如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python? - How can I copy columns from one excel sheet to another with python using openpyxl Library? 如何使用python将Excel工作表复制到具有相同格式的另一个工作簿 - How to copy excel sheet to another workbook with same format using python 使用 python 将列从一个 excel 文件复制到另一个 excel 文件表 - Copy columns from one excel file to another excel file sheet using python 如何使用 Pandas 将具有不同标题的多个 Excel 文件合并到一张表中? - How can I combined multiple Excel files with different headers into one sheet using Pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM