openpyxl

python 操作excel

删除回忆录丶 提交于 2019-12-20 11:00:40
介绍:   python 读写 excel 有好多选择,但是,方便操作的库不多,在我尝试了几个库之后,我觉得两个比较方便的库分别是 xlrd/xlwt、openpyxl。   之所以推荐连个库是因为这两个库分别操作的是不同版本的excel, xlrd 操作的是xls/xlxs 格式,的excel, 而 oppenpyxl 只支持 xlxs 格式的excel, openpyxl 使用起来会更方便一些, 所以如果你操作xlxs 文件的话, 那么可以优先选择openpyxl, 如果要兼容xls的话, 那就用xlrd/xlwt吧 本处使用openpyxl来实现 安装 pip install openpyxl 如果excel 里面有图片(jpg,png,bmg,..........) 需要安装图片处理模块\ pip install pillow excel写 from openpyxl import workbook wb = workbook() 创建一个新的工作薄 ws1 = wb.create_sheet("Mysheet") # 默认最后一个 ws2 = wb.create_sheet("Mysheet", 0) # 第一个 保存 wb.save("balances.xlsx") # 文件名 修改工作薄的名称 ws.title = New Title 获取所有的工作薄名称 print

How to set Background Color of Plot Area of Chart using openpyxl

谁都会走 提交于 2019-12-20 06:32:09
问题 I would like to change the background color of a chart, as in this example, using openpyxl. In a google group discussion I found the following code snippet: from openpyxl.chart.shapes import GraphicalProperties props = GraphicalProperties(solidFill="999999") chart.graphical_properties = props chart.plot_area.graphical_properties = props but it does not have any effect on the chart when saved to the excel file. 回答1: This functionality was seemingly broken in a previous version of openpyxl and

Graphs in xlsx File overwrite by openpyxl

我们两清 提交于 2019-12-20 04:37:12
问题 We need to update xlsx sheet using python script which do some calcualtion and update one worksheet. I choose openpyxl as it supoort writing/updating xlsx File. In the Excel sheet contain some graphs also but When I update excel sheet than graph does not work into excel and data update respectively. I think we had some issue for updating graph with openpyxl.Can anyone provide me some input to fix this issue or In other word,In the Excel sheet I have 10 worksheet.In worksheet 1, it contain

Why is the iteration over this loop not adding cells in openpyxl?

拜拜、爱过 提交于 2019-12-20 03:55:12
问题 Given the following as the contents of the first sheet of an xlsx roi.xlsx: Then: wb = load_workbook('roi.xlsx', data_only=True) ws=wb.worksheets[0] keynames = [i.value for i in ws.columns[0]] I want to add values into column B from the following dict: mydict = {'carnival': 2, 'festival': 3} When I try: for k, v in mydict.items(): keyPosition = keynames.index(k) ws.cell(row = keyPosition, column = 2).value = v I end up with a new column B, but empty values throughout, AND when I go to re-open

Error in converting txt to xlsx using python

爱⌒轻易说出口 提交于 2019-12-20 02:56:07
问题 My code is following. import csv import openpyxl import sys def convert(input_path, output_path): """ Read a csv file (with no quoting), and save its contents in an excel file. """ wb = openpyxl.Workbook() ws = wb.worksheets[0] with open(input_path) as f: reader = csv.reader(f, delimiter='\t', quoting=csv.QUOTE_NONE) for row_index, row in enumerate(reader): for col_index, value in enumerate(row): ws.cell(row=row_index, column=col_index).value = value wb.save(output_path) def main(): try:

Openpyxl updating sheet and maintaining graphs and images

China☆狼群 提交于 2019-12-19 20:38:23
问题 I just created a script with openpyxl to update a xlsx file that we usually update manually every month. It work fine, but the new file lost all the graphs and images that were in the workbook. Is there a way to keep them? 回答1: openpyxl version 2.5 will preserve charts in existing files. 回答2: Use xlwings instead, it works as a wrapper on pywin32 and can preserve the existing Excel graphs, pivot-tables, etc. 来源: https://stackoverflow.com/questions/37214983/openpyxl-updating-sheet-and

Determine if worksheet is empty in openpyxl

扶醉桌前 提交于 2019-12-19 11:22:34
问题 I have an application where I write to a worksheet to the last column + 2 if there is already data and to the last column + 1 if the worksheet is empty. I get what I think is an empty worksheet as follows: from openpyxl.workbook.workbook import Workbook book = Workbook() sheet = book.active When I do sheet.max_row and sheet.max_column , I get 1 for both properties. I would like to get zero, so I do the following: if sheet.max_row == 1 and sheet.max_column == 1 and not sheet['A1'].value: start

adding hyperlinks in some cells openpyxl

丶灬走出姿态 提交于 2019-12-19 05:23:25
问题 I have to generate an excel with summary results. The results are included in a list. Some of the elements are values and some links. I managed to generate the excel with the right format but not generate the hyperlink in some of the cells My try: from openpyxl import Workbook from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Fill from openpyxl.cell import get_column_letter def summaryMCP(self,result): c1=Column('Name',[result[0]]) c2=Column('R2 check',

No module named 'openpyxl' - Python 3.4 - Ubuntu

我与影子孤独终老i 提交于 2019-12-18 18:55:09
问题 I installed openpyxl with $ pip install openpyxl when I try the command from openpyxl import Workbook I get Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> from openpyxl import Workbook ImportError: No module named 'openpyxl' I am using Python 3.4 and Ubuntu 14.04, 32-bit OS type 回答1: @zetysz and @Manish already fixed the problem. I am just putting this in an answer for future reference: pip refers to Python 2 as a default in Ubuntu, this means that pip install x

Python3读取、写入、追加写入Excel文件

只谈情不闲聊 提交于 2019-12-18 12:35:49
一、需要用到的库: 1.操作xls格式的表格文件: 读取:xlrd 写入:xlwt 修改(追加写入):xlutils 2.操作xlsx格式的表格文件: 读取/写入:openpyxl 二、实现代码 1.操作xls格式的表格文件: # coding=UTF-8 import xlrd import xlwt from xlutils.copy import copy def write_excel_xls(path, sheet_name, value): index = len(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格式表格写入数据成功!") def write_excel_xls_append(path, value): index = len(value) # 获取需要写入数据的行数 workbook =