openpyxl

openpyxl get sheet by name

那年仲夏 提交于 2019-11-26 20:38:12
问题 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...)? 回答1: 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

How to access the real value of a cell using the openpyxl module for python

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:56:03
I am having real trouble with this, since the cell.value function returns the formula used for the cell, and I need to extract the result Excel provides after operating. Thank you. Ok, I think I ahve found a way around it; apparently to access cell.internal value you have to use the iter_rows() in your worksheet previously, which is a list of "RawCell". for row in ws.iter_rows(): for cell in row: print cell.internal_value Iulian Stana Like Charlie Clark already suggest you can set data_only on True when you load your workbook: from openpyxl import load_workbook wb = load_workbook("file.xlsx",

Python-22-file文件读取

ぃ、小莉子 提交于 2019-11-26 19:50:12
打开和关闭文件-open with可以自动关闭管道 with open('','r') as fp: pass 不用with,需要关闭 file=open('','r') # 建立管道 content=file.read() # 读入数据 file.close() # 关闭管道 文件如果找不到 #【文件读取,若文件不存在则报错】 with open('datas/test.txt','rb') as fp: data=fp.read() print(data) data_str=data.decode(encoding='UTF-8') print(data_str) #【文件写入,若文件不存在就新建文件】 with open('datas/test_input.txt', 'wb') as fp: fp.write('写入文件'.encode(encoding='UTF-8')) open()参数 filename mode:r、w、a、rb、wb、ab buffer:为0不缓存,为1缓存,为正是缓存区大小,为负是系统默认的大小 encoding:utf-8(8为最小位数,即英文一个字节,8bit) readline和readlines #fp自带迭代器 with open('datas/test.txt','r',encoding='utf-8') as fp:

Fill cells with colors using openpyxl?

孤者浪人 提交于 2019-11-26 19:42:26
问题 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

Save list of DataFrames to multisheet Excel spreadsheet

隐身守侯 提交于 2019-11-26 18:24:40
How can I export a list of DataFrames into one Excel spreadsheet? The docs for to_excel state: Notes If passing an existing ExcelWriter object, then the sheet will be added to the existing workbook. This can be used to save different DataFrames to one workbook writer = ExcelWriter('output.xlsx') df1.to_excel(writer, 'sheet1') df2.to_excel(writer, 'sheet2') writer.save() Following this, I thought I could write a function which saves a list of DataFrames to one spreadsheet as follows: from openpyxl.writer.excel import ExcelWriter def save_xls(list_dfs, xls_path): writer = ExcelWriter(xls_path)

Modify an existing Excel file using Openpyxl in Python

不羁岁月 提交于 2019-11-26 18:10:38
问题 I am basically trying to copy some specific columns from a CSV file and paste those in an existing excel file[*.xlsx] using python. Say for example, you have a CSV file like this : col_1 col_2 col_3 col_4 1 2 3 4 5 6 7 8 9 10 11 12 So, i wanted to copy the both col_3 and col_4 and paste those in col_8 and col_9 in an existing excel file [which is a .XLSX format]. I have tried this in various way to solve, but could not find out the exact way. i tried something like this : with open( read_x

Editing workbooks with rich text in openpyxl

天涯浪子 提交于 2019-11-26 17:57:58
I was wondering if openpyxl can read and/or write rich text into excel. I am aware that this question was asked once before in 2012 linked below, but I am not sure if this has changed. As it stands load_workbook() seems to throw away rich text formatting. As for a specific problem, I need to open, edit, and save a workbook where some cells have both superscripted and normal text in one cell. When I save the workbook, the format of the first character of the cell is applied to the rest of the cell. Here is the to 2012 question: How do I find the formatting for a subset of text in an Excel

python处理Excel文件的几个模块

不打扰是莪最后的温柔 提交于 2019-11-26 17:37:30
在python中简单地处理excel文件,有几个相关的模块,各有千秋,本文将不定时收录。 Python Excel网站 收集了关于python处理excel文件的各种信息。 【注意】使用python处理excel文件前,请多备份文件,以防数据丢失。 ------------------ 0x01 xlrd xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files. 官方文档: https://xlrd.readthedocs.io/en/latest/api.html github项目: https://github.com/python-excel/xlrd 安装: pip install xlrd 使用: 只能读.xls、.xlsx文件(xlrd0.8.0+版本支持读取xlsx文件) import xlrd book = xlrd.open_workbook("pcat.xls") print("The number of worksheets is {0}".format(book.nsheets)) print("Worksheet name(s): {0}".format(book.sheet

Setting styles in Openpyxl

人走茶凉 提交于 2019-11-26 15:45:02
问题 I need advice on setting styles in Openpyxl. I see that the NumberFormat of a cell can be set, but I also require setting of font colors and attributes (bold etc). There is a style.py class but it seems I can't set the style attribute of a cell, and I don't really want to start tinkering with the openpyxl source code. Has anyone found a solution to this? 回答1: As of openpyxl version 1.5.7, I have successfully applied the following worksheet style options... from openpyxl.reader.excel import

How to save XLSM file with Macro, using openpyxl

南笙酒味 提交于 2019-11-26 14:48:50
问题 I have .xlsm file with a Macro function. I'm loading it using openpyxl and write some data to the file and finally want to save as a different .xlsm file. To save the file as XLSM file I have used below code in my Python script. wb.save('testsave.xlsm'); But I cannot open that file if I saved as above. But if I saved it as .xlsx then I can open the file without the Macro function that original file had. I want to open a Excel sheet that has Macro function, edit the file and save it as new