我正在尝试使用openpyxl模块写入* .xlsx文件。我在Ubuntu 14.04上使用pip install下载了它。
我正在加载一个* .xlsx文件,这是我以前在Excel中使用的Bill of Materials模板,我可以在Libre Office和Kingsoft Office中打开它。我的意图是加载它并用一些字符串填充一些单元格。
我的功能如下:
def writeBOM(parts, projectname):
'''
Take the ordered and grouped part info and
write it to a standard BOM and save it
'''
StandardBOMFILE = '/home/jesse/Digi-Parser/SampleFiles/StandardBOM.xlsx'
wb = load_workbook(filename=StandardBOMFILE)
sheet = wb.get_sheet_by_name('BOM')
r = 8
# Fill BOM
for i, part in enumerate(parts):
sheet.cell(row = r+i,column = 1).value = part.designator
sheet.cell(row = r+i,column = 2).value = part.evalue + ' ' + part.package
sheet.cell(row = r+i, column = 3).value = part.qty
projectBOMname = projectname + 'BOM' + '.xlsx'
wb.save(projectBOMname)
我放入单元格的值只是字符串。
然而,当我运行此操作时,我收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/home/jesse/Digi-Parser/SheetOrganizer.py in <module>()
232 prjpath = '/home/jesse/Digi-Parser/SampleFiles/'
233 prjname = 'Water Use Monitor'
--> 234 things = csvToBOM(prjpath, prjname)
/home/jesse/Digi-Parser/SheetOrganizer.py in csvToBOM(projectpath, projectname)
223 orderedBody = combineSameComponents(reorderParts(body))
224
--> 225 writeBOM(orderedBody, projectname)
226
227
/home/jesse/Digi-Parser/SheetOrganizer.py in writeBOM(parts, projectname)
192 sheet.cell(row = r+i, column = 3).value = part.qty
193 projectBOMname = projectname + 'BOM' + '.xlsx'
--> 194 wb.save(projectBOMname)
195
196
/usr/local/lib/python2.7/dist-packages/openpyxl/workbook/workbook.pyc in save(self, filename)
265 save_dump(self, filename)
266 else:
--> 267 save_workbook(self, filename)
/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.pyc in save_workbook(workbook, filename)
183 """
184 writer = ExcelWriter(workbook)
--> 185 writer.save(filename)
186 return True
187
/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.pyc in save(self, filename)
166 """Write data into the archive."""
167 archive = ZipFile(filename, 'w', ZIP_DEFLATED)
--> 168 self.write_data(archive)
169 archive.close()
170
/usr/local/lib/python2.7/dist-packages/openpyxl/writer/excel.pyc in write_data(self, archive)
78 archive.writestr(ARC_WORKBOOK_RELS, write_workbook_rels(self.workbook))
79 archive.writestr(ARC_APP, write_properties_app(self.workbook))
---> 80 archive.writestr(ARC_CORE, write_properties_core(self.workbook.properties))
81 if self.workbook.loaded_theme:
82 archive.writestr(ARC_THEME, self.workbook.loaded_theme)
/usr/local/lib/python2.7/dist-packages/openpyxl/writer/workbook.pyc in write_properties_core(properties)
65 SubElement(root, '{%s}created' % DCTERMS_NS,
66 {'{%s}type' % XSI_NS: '%s:W3CDTF' % DCTERMS_PREFIX}).text = \
---> 67 datetime_to_W3CDTF(properties.created)
68 SubElement(root, '{%s}modified' % DCTERMS_NS,
69 {'{%s}type' % XSI_NS: '%s:W3CDTF' % DCTERMS_PREFIX}).text = \
/usr/local/lib/python2.7/dist-packages/openpyxl/date_time.pyc in datetime_to_W3CDTF(dt)
54 def datetime_to_W3CDTF(dt):
55 """Convert from a datetime to a timestamp string."""
---> 56 return datetime.datetime.strftime(dt, W3CDTF_FORMAT)
57
58
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900
我无法弄清楚如何解决这个问题。我不需要在我的电脑上安装Excel吗?似乎问题出在openpyxl包中的date_time.py文件中,而变量&#39; dt&#39;由于某种原因,它被设定为1899年。
感谢您的帮助。
答案 0 :(得分:0)
正如邮件列表中所讨论的,这看起来像Python日期时间库中的错误。 https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
Python 3.3或3.4
没有问题