openpyxl

Multiple styles in one cell in openpyxl

可紊 提交于 2019-12-05 21:50:45
I wrote a Python program which produces invoices in a specific form as .xlsx files using openpyxl. I have the general invoice form as an Excel workbook and my program copies this form and fills up the details about the specific client (eg. client refernce number, price, etc.) which are read from another .txt file. The program works perfectly. The only problem is that the form contains a cell which has multiple styles: half of the letters are red and the rest black and there is also size difference. This cell is not edited in my program (it is the same in all the invoices), however after the

How to iterate over worksheets in workbook, openpyxl

耗尽温柔 提交于 2019-12-05 19:51:10
问题 I've been using the openpyxl module to do some processing on some .xlsx files. I've been trying to figure out how to iterate over sheets in a workbook. I'm not sure if I can get it figured out. I've tried the 2 codes below which both return empty results. My .xlsx file has about 20 sheets, so something should return. The one thing I couldn't find on the internet, is how to set a workbook to an actual workbook. Usually I am writing to a workbook, so I just initialize it by setting a variable

用Python在Excel里画出蒙娜丽莎

我怕爱的太早我们不能终老 提交于 2019-12-05 19:32:58
前言 文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。 作者: 麦麦麦造 PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef 基本思路 实现这个需求的基本思路是读取这张图片每一个像素的色彩值,然后给excel里的每一个单元格填充上颜色。所以主要用到的是PIL、openpyxl这两个库。 PIL使用 PIL是Python里面做图像处理的时候十分常用的一个库,功能也是十分的强大,这里只需要用到PIL里一小部分的功能。 1 from PIL import Image 2 img = Image.open(img_path) # 读取图片 3 width, height = img.size # 获取图片大小 4 r, g, b = img.getpixel((w - 1, h - 1)) # 获取像素色彩值 Image.open() 是PIL里面打开一张图片的函数,支持多种图片类型 img_path 是图片路径,可以是相对路径,也可以是绝对路径 img.size 是获取图片的size属性,包含图片的宽和高 img.getpixel() 是获取图片色彩值的函数

27.openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽

倖福魔咒の 提交于 2019-12-05 17:51:37
openpyxl 向指定单元格添加图片并修改图片大小 以及修改单元格行高列宽 from openpyxl import Workbook,load_workbook from openpyxl.drawing.image import Image import os wb = Workbook() sheet=wb.active # 设置文字图片单元格的行高列宽 column_width=10 row_height=80 # 设置行高,该设置的行高与excel文件中设置的行高值是一样的 path=os.getcwd() # 输出当前目录 img_list=os.listdir(path) for r,file in enumerate(img_list,1): jpg=os.path.splitext(file)[1] # 分割文件,并将后缀名提取出来 if jpg=='.jpg': # 下面代码中的[]括号中可以输入'D'或者'd' sheet.column_dimensions['D'].width=column_width # 修改列D的列宽 sheet.row_dimensions[r].height=row_height # 修改行3的行高 img=Image(file) # 调用图像函数 newSize=(90,90) img.width,img.height

Openpyxl How to get row from worksheet by index

纵然是瞬间 提交于 2019-12-05 17:49:13
问题 Using Openpyxl and python3.5, I tried getting the first row from an excel worksheet using a subscript but I an error. # after getting filename # after loading worksheet # to get the first row of the worksheet first_row = worksheet.rows[0] # I get Traceback (most recent call last): File "<pyshell#54>", line 1, in <module> first_row = phc_th_sheet.rows[1] TypeError: 'generator' object is not subscriptable In relation to getting the first row, I've also tried first_row = worksheet.(row=1) # and

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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 17:43: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格式表格写入数据成功!") #

pandas: Writing to an existing excel file (xlsx) using to_excel

感情迁移 提交于 2019-12-05 16:40:21
I have a simple use case for df.to_excel() that I'm struggling with. I want to write to a specific worksheet tab (let's call it "Data") of an existing XLSX workbook, which could be referenced by formulas and pivots on other tabs. I've tried to modify ExcelWriter in two ways but both produce errors from openpyxl. Read an existing sheet using get_sheet_by_name (This errors: "NotImplementedError: use 'iter_rows()' instead".) Create a new sheet using create_sheet. (This errors:"ReadOnlyWorkbookException: Cannot create new sheet in a read-only workbook") df=DataFrame() from openpyxl.reader.excel

openpyxl 2.4.2: cell value generated by formula empty after saving

ε祈祈猫儿з 提交于 2019-12-05 14:37:15
I use openpyxl to open a file, edit some cells and save the changes. Here's an example: import openpyxl book = openpyxl.load_workbook(sheet_path) sheet = book.active for row in range(sheet.max_row): index = row + 1 sheet.cell(row=index, column=1).value = "something" book.save(sheet_path) The problem is, when I save the file, other cells are modified. In fact, the cells in my sheet that contains formulas are "corrupted", the file size is greatly reduced and when I use other scripts to read the sheet, the cells containing formulas are reported as empty. But when I open the sheet, everything

Trouble with applying Styles in - OpenPyXL

一个人想着一个人 提交于 2019-12-05 14:31:26
I am trying to style specific rows and columns. worksheet.cell(row=file_row_number, column=1).value = "Hotel ID" _cell = worksheet.cell("C1") _cell.style.font.bold = True It shows me error TypeError: cannot set bold attribute Previously I was using XLWT and it had very easy method of applying styles like you define style variable once and then for ever write() yo can just pass the style variable to apply style on that row/column Is there any way in OpenPyXL to apply style easily as I mentioned above? As explained here , you should first create a Style object: from openpyxl.styles import Style,

Hyperlink style in Openpyxl

a 夏天 提交于 2019-12-05 13:06:49
In openpyxl, you can set a hyperlink like this: cell.hyperlink = r'..\somedir\somefile.txt' However, this does not apply the hyperlink style that you would get when setting a hyperlink in Excel. You can apply a style with blue text and underlining, but when you open the document, visited hyperlinks do not change color. Is there any way with openpyxl to specify a hyperlink style that works like a hyperlink set within Excel? You have to change style attribute cell.style = "Hyperlink" import openpyxl from openpyxl.styles import Font, Color, colors #... # alternative 1: set hyperlink property to