openpyxl

python操作excel——openpyxl

╄→尐↘猪︶ㄣ 提交于 2019-11-28 01:32:04
一、概述    python操作excel各个库对比: https://www.cnblogs.com/paul-liang/p/9187503.html   官方文档: https://openpyxl.readthedocs.io/en/stable/#usage-examples 二、入门    1.安装: pip install openpyxl   conda: conda install openpyxl    2.创建excel文件 from openpyxl import Workbook # 实例化一个工作簿 wb = Workbook() # 激活工作簿 ws = wb.active    3.打开已有文件 from openpyxl import load_workbook wb2 = load_workbook('文件名称.xlsx')    4.单元格赋值 ws['A1'] = 42   5.单元格访问 c = ws['A4'] d = ws.cell(row=4, column=2, value=10)    6.保存文件 wb.save('文件名称.xlsx')      更多参考: https://blog.csdn.net/weixin_43094965/article/details/82226263 来源: https://www

The ability to apply multiple formats to cell with xlwt / openpyxl

廉价感情. 提交于 2019-11-28 00:51:39
问题 I plan to use one of 2 libraries below to output excel file in python: xlwt ( http://www.python-excel.org/ ) openpyxl ( http://packages.python.org/openpyxl/ ) I tried the first one, most of things seem to be fine but one issue, unfortunately it may not support the ability to apply multiple formats to cell. (see http://groups.google.com/group/python-excel/browse_thread/thread/11c24606d9b2914d) Is it true? If yes, does anybody know how to solve it? E.g it cannot make some words bold, others

Insert column using openpyxl

别等时光非礼了梦想. 提交于 2019-11-27 23:55:44
问题 I'm working on a script that modifies an existing excel document and I need to have the ability to insert a column between two other columns like the VBA macro command .EntireColumn.Insert . Is there any method with openpyxl to insert a column like this? If not, any advice on writing one? 回答1: Haven't found anything like .EntireColumn.Insert in openpyxl. First thought coming into my mind is to insert column manually by modifying _cells on a worksheet. I don't think it's the best way to insert

How to write a list to xlsx using openpyxl

眉间皱痕 提交于 2019-11-27 23:19:54
I have a list of some values in Python and want to write them into an Excel-Spreadsheet using openpyxl. So far I tried, where lstStat is a list of integers that needs to be written to the Excel file: for statN in lstStat: for line in ws.range('A3:A14'): for cell in line: cell.value(statN) I'm getting a TypeError: 'NoneType' object is not callable for the last line in the code snipet. Can you help me out how to write my data to the Excel file? Cheers Thomas To assign a value to a cell, use = : cell.value = statN You also need to fix your loops. Notice that right now, for each element in lstStat

xpython操作excel之xlwt与xlrd

别来无恙 提交于 2019-11-27 21:38:51
基于rest_framework框架的二进制流数据传输(提供较大文件的下载)   (1)数据源:     高质量图片、视频、音频、文件、数据库数据等。如果是数据库文件,需要先读取相应的数据,然后写入表格在传输到前端以供下载!   (2)数据读取:     利用yield函数生成器进行rb模式文件读取操作   (3)数据传输:     通过StreamingHttpResponse()二进制流格式进行相应(from django.http.response import StreamingHttpResponse)   如果除数中使用的有中文,需要通过quote()方法来确保中文不乱码(from urllib.parse import quote) 文件的上传见< Django之AJAX文件上传 >篇,python操作excel表格见< python操作excel----openpyxl模块 >或< xpython操作excel之xlwt与xlrd > drf接口模式提供唯一码的下载案例    1 import os 2 import openpyxl 3 from rest_framework.views import APIView 4 from django.http.response import StreamingHttpResponse 5 from urllib

openpyxl get sheet by name

…衆ロ難τιáo~ 提交于 2019-11-27 21:20:19
I am writing some data into an Excel file, but I dont know how to adjust the code in order to be able to control which sheet I am writing into: wb = load_workbook(filename) active_ws = wb.active Instead of wb.active , how can I say something like Sheets('Data') (this is how the VBA syntax would look like...)? Valeriy Solovyov You should use wb[sheetname] from openpyxl import load_workbook wb2 = load_workbook('test.xlsx') ws4 = wb2["New Title"] PS: You should check if your sheet in sheet names wb.sheetnames print(wb2.sheetnames) ['Sheet2', 'New Title', 'Sheet1'] import openpyxl n = 0 wb =

Is it possible to get an Excel document's row count without loading the entire document into memory?

筅森魡賤 提交于 2019-11-27 19:36:49
I'm working on an application that processes huge Excel 2007 files, and I'm using OpenPyXL to do it. OpenPyXL has two different methods of reading an Excel file - one "normal" method where the entire document is loaded into memory at once, and one method where iterators are used to read row-by-row. The problem is that when I'm using the iterator method, I don't get any document meta-data like column widths and row/column count, and i really need this data. I assume this data is stored in the Excel document close to the top, so it shouldn't be necessary to load the whole 10MB file into memory

python - fill cells with colors using openpyxl

百般思念 提交于 2019-11-27 19:00:52
I am currently using openpyxl v2.2.2 for Python 2.7 and i wanted to set colors to cells. I have used the following imports import openpyxl, from openpyxl import Workbook from openpyxl.styles import Color, PatternFill, Font, Border from openpyxl.styles import colors from openpyxl.cell import Cell and the following is the code I tried using: wb = openpyxl.Workbook() ws = wb.active redFill = PatternFill(start_color='FFFF0000', end_color='FFFF0000', fill_type='solid') ws['A1'].style = redFill but I get the following error: Traceback (most recent call last) self.font = value.font.copy()

Get cell color from .xlsx

微笑、不失礼 提交于 2019-11-27 18:52:38
问题 I am using openpyxl to read excel file. I want to get cell color from "xlsx" file. I tried to get color so: wb = load_workbook('Test.xlsx', data_only=True) sh = wb[Sheet1] sh['A1'].fill.start_color.index #Green Color I get "11L", but i need to get rgb color, how i can do it? 回答1: Looks like the sheet is using the built-on colour index. The mapping for these is in the source of openpyxl.styles.color COLOR_INDEX = ( '00000000', '00FFFFFF', '00FF0000', '0000FF00', '000000FF', #0-4 '00FFFF00',

python3读取、写入、追加写入excel文件

别来无恙 提交于 2019-11-27 18:46:57
由于excel版本不同,python处理的时候选择的库页不同。 一、操作对应版本表格需要用到的库 1、操作xls格式的表格文件,需要用到的库如下: 读取:xlrd 写入:xlwt 修改(追加写入):xlutils 2、操作xlsx格式的表格文件,需要用到的库如下: 读取/写入:openpyxl (好像对于xlsx格式的表格,使用xlrd也是可以读取的,只是写入会有问题,不过避免问题还是根据不同格式的表格选择对应的库吧~) 二、实现代码 1、xlwt写入xls文件内容 import xlwt def write_excel_xls(path, sheet_name, value): index = len(value) # 获取需要写入数据的行数(value是个二维数组) workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(sheet_name) # 在工作簿中新建一个表格 for i in range(0, index): for j in range(0, len(value[i])): sheet.write(i, j, value[i][j]) # 像表格中写入数据(对应的行和列) workbook.save(path) # 保存工作簿 print("xls格式表格写入数据成功!") #