[英]Python - Excel - Add sheet to existing workbook without removing sheets
Context: I am attempting to automate a report that is rather complicated (not conceptually, just in the sheer volume of things to keep track of).上下文:我试图自动化一个相当复杂的报告(不是概念上的,只是为了跟踪大量的事情)。 The method I settled on after a lot of investigation was to;经过大量调查,我确定的方法是;
Obviously, I have spent a lot of time looking at other peoples solutions to this issue (as this topic has been discussed a lot).显然,我花了很多时间查看其他人对这个问题的解决方案(因为这个话题已经讨论了很多)。 The issue I have found however is that (at least in my search) none of the methods purposed have worked for me, my belief is that the previously correct responses are no longer relevant in current versions of pandas etc. Rather than linking to the dozens of articles attempting to answer this question, I will explain the issues I have had with various solutions.然而,我发现的问题是(至少在我的搜索中)没有一种方法对我有用,我相信以前正确的响应在当前版本的熊猫等中不再相关。而不是链接到几十个在试图回答这个问题的文章中,我将解释我在各种解决方案中遇到的问题。
Below is a heavily redacted version of the code which should give all the required detail.下面是代码的大量编辑版本,它应该提供所有必需的细节。
#Imports
import os
import pyodbc
import numpy as np
import shutil
import pandas as pd
import datetime
from datetime import date
from openpyxl import load_workbook
# Set database connection variables.
cnxn = pyodbc.connect(*Credentials*)
cursor = cnxn.cursor()
df = pd.read_sql_query(script, cnxn)
df.to_excel(writer, sheet_name = 'Some Sheet',index=False)
writer.close()
Long story short here, I am finding it very frustrating that what should be very very simple is turning into a multiple day long exercise.长话短说,我觉得非常令人沮丧的是,本来应该非常非常简单的事情却变成了为期多天的练习。 Please if anyone has experience with this and could offer some insight I would be very grateful.请如果有人有这方面的经验并能提供一些见解,我将不胜感激。
Finally, I have to admit that I am quite new to using python, though I have not found the transition too difficult until today.最后,我不得不承认我对使用 python 还很陌生,尽管直到今天我才发现过渡太困难。 Most of the issues I have been having are easily solvable (for me), with the exception of this issue.我遇到的大多数问题都很容易解决(对我来说),除了这个问题。 If there is something I have somehow completely missed, put me on the track and I will not be a bother.如果有什么我完全错过了,把我放在赛道上,我就不会打扰了。
Okay, so I found that I was infact incorrect (big surprise).好的,所以我发现我实际上是不正确的(大惊喜)。 That is, my statement that the helper function does not work.也就是说,我说辅助函数不起作用。 It does work, the ZipFile issue was most likely caused by some form of protection on the workbook.它确实有效,ZipFile 问题很可能是由工作簿上的某种形式的保护引起的。 Funny thing is, I was able to get it working with a new workbook, but when I changed the name of the new workbook it again started throwing the ZipFile error.有趣的是,我能够让它与新工作簿一起工作,但是当我更改新工作簿的名称时,它再次开始抛出 ZipFile 错误。 After awhile of creating new files and trying different things I eventually got it to work.在创建新文件并尝试不同的事情一段时间后,我最终让它工作了。
Two things I would note about the helper function;关于辅助函数,我要注意两件事;
Hope this helps anyone running into the same issue.希望这可以帮助任何遇到相同问题的人。
Lesson learned, as always the information is out there, its just a matter of actually paying close attention and trying things out rather than copy paste and scratching your head when things aren't working.吸取的教训,一如既往,信息就在那里,这只是一个实际密切关注和尝试的问题,而不是在事情不工作时复制粘贴和挠头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.