xlrd

python读取excel保存到mysql

自作多情 提交于 2021-02-14 09:00:06
首先安装xlrd模块:pip install xlrd ,核心代码网上有很多,这里主要是关于一些个人实际碰到问题细节的处理 1、excel数据不规范导致读取的数据存在空白行和列; 2、参数化执行sql 代码如下,仅供参考: 1 import xlrd 2 3 import AppSetting.AppConfig as config 4 import AppSetting.dbConfig as db 5 6 # 处理excel依赖xlrd模块 pip install xlrd 7 8 # 读取excel文件 9 excel_data = xlrd.open_workbook(config.file_path) 10 # 获取第一个sheet页 11 sheet = excel_data.sheet_by_index(0) 12 # 总行数 13 rows = sheet.nrows 14 # 获取列(经常读取到的excel可能存在空白行或者空白列,这里根据第一行的数据获取要导入的数据的列数) 15 rowlsts = [i for i in sheet.row_values(0) if i != '' ] 16 cursor = db.connect.cursor() 17 # 定义变量n,当n=0 时组装参数化的sql 18 n = 0 19 sql = "" 20 #

Python IDLE, importing XLRD, error generated in debug mode: AttributeError: '_ModuleLock' object has no attribute 'name'

我只是一个虾纸丫 提交于 2021-02-11 14:06:26
问题 I'm a newbie to Python and this is the first time I've tried debug mode. I've read the answers to almost similar questions on stackoverflow but none of them seem to address this situation: When I run this code in debug mode (IDLE, Windows 10): import xlrd print('Hello World!') and set a break-point on print('Hello World") and try to step through the code, I get the following error lines: > **Traceback (most recent call last): File "D:/data/python/hello_world.py", line 4, in <module> > import

How to use xlrd for writing an excel file

南笙酒味 提交于 2021-02-08 07:47:39
问题 How can I write an excel file(xls/xlsx) using xlrd module alone? I tried from xlrd import xlsx , but couldn't find anything that will really help me. 回答1: xlrd only reads excel files. To write them, look up xlwt, xlutils, xlsxwriter, or openpyxl - all of these packages can write binary files excel can read. Excel can also read csv files, which the csv package (included with Python) can write (and read). 来源: https://stackoverflow.com/questions/41886791/how-to-use-xlrd-for-writing-an-excel-file

'utf-16-le' codec can't decode bytes while reading EXCEL in PYTHON

僤鯓⒐⒋嵵緔 提交于 2021-02-05 09:28:10
问题 I am trying to read various numbers of xls files with different languages, Arabic, Greek, Italian, Hebrew, etc. and I get the error shown below when I try to call open_workbook function, any idea how can I set the format to any language? Code: book = xlrd.open_workbook(workbook_url) Error: return codecs.utf_16_le_decode(input, errors, True) UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 372-373: unexpected end of data 回答1: It's unlikely that language is the issue. More

python: converting corrupt xls file

北城以北 提交于 2021-02-04 19:41:05
问题 I have downloaded few sales dataset from a SAP application. SAP has automatically converted the data to .XLS file. Whenever I open it using Pandas library I am getting the following error: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\xff\xfe\r\x00\n\x00\r\x00' When I opened the .XLS file using MSEXCEL it is shows a popup saying that the file is corrupt or unsupported extension do you want to continue when I clicked 'Yes' its showing the correct data. When I

Python: read a percentage value from excel using xlrd

余生颓废 提交于 2021-01-29 07:35:35
问题 I am trying to read percentage value from excel using xlrd. but the output keeps coming as 1.0. Is there a way that I can load it as a percentage without the value being changed? Below is what I have in my excel sheet code: for r in range(1, xlsheet.nrows): num_cols = xlsheet.ncols print('-'*40) print('Row: %s' % r) for col_idx in range(1, num_cols):​for r in range(1, xlsheet.nrows): num_cols = xlsheet.ncols print('-'*40) print('Row: %s' % r) for col_idx in range(1, num_cols): cell_obj =

Why is python xlrd errors when opening a .xlsm instead of .xls

本小妞迷上赌 提交于 2021-01-28 05:24:54
问题 Python program opens .xls just fine but will not open .xlsm and immediately fails. xlrd.open_workbook("Some filename.xlsm") Error: Exception has occurred: AttributeError 'bytes' object has no attribute 'seek' Any help would be greatly appreciated... 回答1: For those who run into this. The solution is to read the file via binary into a variable to avoid the open_workbook library for loading the file... it has something to do with the file coded utf-16-le i think... anyways here is a snippet of

Python Xlrd read excel file error

孤街浪徒 提交于 2021-01-28 04:46:10
问题 I have been working on a script on Windows and now when changing the platform to Linux/Ubuntu I am having some issues. In the example below I am just trying to print the sheetnames but receicing some errors which I have never seen before. May this be a issue with Xlrd ? Because I am able to open the Excel file in Libreoffice. Code: #!/usr/bin/python import xlrd xl_workbook = xlrd.open_workbook('/home/ubuntu/Downloads/usage01.12.2015_31.12.2015.xls') sheet_names = xl_workbook.sheet_names()

using Pandas to read in excel file from URL - XLRDError

寵の児 提交于 2021-01-27 11:05:33
问题 I am trying to read in excel files to Pandas from the following URLs: url1 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/CTA_Historical.xls' url2 = 'https://cib.societegenerale.com/fileadmin/indices_feeds/STTI_Historical.xls' using the code: pd.read_excel(url1) However it doesn't work and I get the error: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '2000/01/' After searching on Google it seems that sometimes .xls files offered through URLs are

Open BytesIO (xlsx) with xlrd

二次信任 提交于 2021-01-27 05:21:11
问题 I'm working with Django and need to read the sheets and cells of an uploaded xlsx file. It should be possible with xlrd but because the file has to stay in memory and may not be saved to a location I'm not sure how to continue. The start point in this case is a web page with an upload input and a submit button. When submitted the file is caught with request.FILES['xlsx_file'].file and send to a processing class that would have to extract all the important data for further processing. The type