简体   繁体   English

图形覆盖到Python中的现有excel文件时丢失

[英]Graphs lost while overwriting to existing excel file in Python

I'm using openpyxl to write to an existing file and everything works fine. 我正在使用openpyxl写入现有文件,并且一切正常。 However after the data is saved on the file, graphs disappear. 但是,将数据保存到文件后,图形消失。

I understand Openpyxl currently only supports chart creation within a worksheet only. 我了解Openpyxl当前仅支持在工作表中创建图表。 Charts in existing workbooks will be lost. 现有工作簿中的图表将丢失。

Are there any alternate libraries in Python to achieve this. Python中是否有其他替代库可以实现此目的。 I just want to feed a few values, so all the graphs and calculation happen in excel. 我只想输入一些值,所以所有图形和计算都在excel中进行。

Thank you. 谢谢。

目前(2.2版)是不可能的。

I got some alternative solution to execute excel macro from python, that might be the solution for the above problem. 我有一些替代解决方案,可以从python执行excel宏,这可能是上述问题的解决方案。

Create a ExcelWorkbook.xlsm and write the excel macro(which is very easy, excel macro recoding might help you) of what ever task you want to do and execute the macro from python. 创建一个ExcelWorkbook.xlsm并编写要执行的任务的excel宏(这非常简单,excel宏重新编码可能会帮助您)并从python执行该宏。 Graph and shape object will be safe. 图形和形状对象将是安全的。

openpyxl can be used to write and read the ExcelWorkbook.xlsm openpyxl可用于编写和读取ExcelWorkbook.xlsm

from openpyxl import Workbook, load_workbook
import os
import win32com.client

#The excel macro should be written and saved (Excelworkbook.xlsm) before using this code. 
#Use macro codeExcelworkbook.xlsm to edit ExcelWorkBookContainGraph.xlsx
##################### Openpyxl #####################
#Open the Excelworkbook.xlsm (Macro workbook)
wb = load_workbook(filename='Excelworkbook.xlsm', read_only=False, keep_vba=True)
ws = wb.worksheets[0] #Worksheet will be sheet1[0]

##### Do the required task (read, write, copy..etc) in excel using openpyxl #####

#save Excelworkbook.xlsm
wb.save('Excelworkbook.xlsm')

#################### Run the excel macro #####################
if os.path.exists("Excelworkbook.xlsm"):
    xl=win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\Excelworkbook.xlsm")#, ReadOnly=1)
    xl.Application.Run("Excelworkbook.xlsm!Module1.Macro1")
    xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
    xl.Application.Quit() # Comment this out if your excel script closes
    del xl  

############### Working with excel contains Graph ###############
xl=win32com.client.Dispatch("Excel.Application")
xl.Workbooks.Open(Filename=os.path.dirname(os.path.abspath(__file__))+"\ExcelWorkBookContainGraph.xlsx")#, ReadOnly=1)
try:
    xl.ActiveWorkbook.SaveAs("C:\ExcelExample\ExcelWorkBookContainGraph.xlsx")#Change the save path as per your requirment
    xl.Application.Quit() # Comment this out if your excel script closes
    del xl
except:
    #If you get some error while saving kill the running excel task in background
    print 'Error in saving file'
    xl.Application.Quit() # Comment this out if your excel script closes
    del xl  

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM