我的代码:
import xlrd
wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls")
sh = wb.sheet_by_index(0)
print sh.cell(0,0).value
Run Code Online (Sandbox Code Playgroud)
错误:
Traceback (most recent call last):
File "Z:\Wilson\tradedStockStatus.py", line 18, in <module>
wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls")
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 429, in open_workbook
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1545, in getbof
bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1539, in bof_error
raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record;
found '<table r'"
Run Code Online (Sandbox Code Playgroud)
该文件似乎没有损坏或格式不同.任何有助于找到问题根源的事情都会很棒.
我正在尝试加载当前存在的工作表并导入下面显示的文本文件(逗号分隔值)截图,
Excel表格:

文本文件:

我使用下面显示的代码:
# importing necessary modules for performing the required operation
import glob
import csv
from openpyxl import load_workbook
import xlwt
#read the text file(s) using the CSV modules and read the dilimiters and quoutechar
for filename in glob.glob("E:\Scripting_Test\Phase1\*.txt"):
spamReader = csv.reader((open(filename, 'rb')), delimiter=',')
#read the excel file and using xlwt modules and set the active sheet
wb = load_workbook(filename=r"E:\Scripting_Test\SeqTem\Seq0001.xls")
ws = wb.worksheets(0)
#write the data that is in text file to excel file
for rowx, row in …Run Code Online (Sandbox Code Playgroud) 我有一个Python ExcelDocument类,它提供了读取/写入/格式化Excel文件的基本方便方法,我在看似简单的Python代码中遇到了一个奇怪的错误.我有一个保存和saveAs方法:
def save(self):
''' Save the file '''
self.workbook.Save()
def saveAs(self, newFileName):
''' Save the file as a new file with a different name '''
self.workbook.SaveAs(newFileName)
Run Code Online (Sandbox Code Playgroud)
save方法工作正常,但是当我尝试调用saveAs方法时myExcelObject.saveAs("C:/test.xlsx")- 我收到以下错误:
Traceback (most recent call last):
File "C:\workspace\Utilities\src\util\excel.py", line 201, in <module>
excel.saveAs("C:/test.xlx")
File "C:\workspace\Utilities\src\util\excel.py", line 185, in saveAs
self.workbook.SaveAs(newFileName)
File "<COMObject Open>", line 7, in SaveAs
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office Excel', u"Microsoft Office Excel cannot access the file 'C:\\//8CBD2000'. There are …Run Code Online (Sandbox Code Playgroud) 我写了一个烧瓶应用程序,将用于处理一些 excel 文件,但是,我为 .xlsx 文件编写了它。输入的文件可能是 .xls 文件,我知道 Openpyxl 无法打开。在我的应用程序中使用 Openpyxl 处理文件之前,如何将文件转换为 .xlsx?
我在网上看到了一些关于使用 xlrd 将原始 .xls 写入 Openpyxl 可以处理的 .xlsx 文件的内容,但是我在调整它以适合我的特定应用程序时遇到了麻烦。
提前致谢!
from openpyxl import load_workbook
from openpyxl.styles import Style, Font
from flask import Flask, request, render_template, redirect, url_for, send_file
import os
app = Flask(__name__)
@app.route('/')
def index():
return """<center><body bgcolor="#FACC2E">
<h1><p>Automated TDX Report</h1></p><br>
<form action="/upload" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>
</form></center></body>"""
@app.route('/upload', methods = ['GET', 'POST'])
def upload():
if request.method == 'POST':
f = request.files['file']
f.save(f.filename) …Run Code Online (Sandbox Code Playgroud)