【问题标题】:Append output to an existing Excel File using OpenPyXL使用 OpenPyXL 将输出附加到现有 Excel 文件
【发布时间】:2015-11-10 17:25:03
【问题描述】:

第 1 步。从 excel 文件中获取输入。

第 2 步。创建 WB 对象。

第 3 步。从该 WB 对象中获取数据并创建一个字典列表。

第 4 步。操作、格式化、设置数据样式。

第 5 步。现在需要输出数据并将其附加到现有 Excel 工作簿中。 这意味着现有 Excel 工作簿将附加来自 第 1 步不断。

我已经完成了第 1 - 4 步,并且可以将我的数据输出到一个新的工作簿。我来了 第 5 步为空。

任何指导或方向将不胜感激。

###  Python 3.X  ###
import sys
import time
import openpyxl
from openpyxl.styles import Alignment, Font, Style
from openpyxl.cell import get_column_letter
from pathlib import Path

###Double Click a Batch file, CMD opens, Client drags and drops excel File            
###to be Formatted

try:
    path = sys.argv[1]
except IndexError:
    path = Path(input('Input file to read: ').strip("'").strip('"'))

output_string = str(Path(path.parent, path.stem + '.NewFormatted.xlsx'))

wb = openpyxl.load_workbook(str(path))
sheet1 = wb.worksheet[0]

###Do the Python Dance on Data###

###Style, Font, Alignment applied to sheets###

###Currently Saves output as a NEW excel File in the Directory the original    
###file was drag and dropped from

wb.save(output_string)

###Need the output to be appended to an existing Excel file already present      
###in a target directory

###Note Formatted Workbook (output) has multiple sheets###

【问题讨论】:

    标签: python excel append openpyxl


    【解决方案1】:

    openpyxl 将允许您编辑现有工作簿,包括将数据附加到它们。但是提供的方法仅限于单个数据项,例如单元格。没有用于将工作表等内容从一个工作簿复制到另一个工作簿的聚合函数。

    【讨论】:

    • 非常感谢您的回复。这很不幸,因为现在我需要找到另一种方法来获取 Excel 数据并输出到现有的 Excel 文件。
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多