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