openpyxl

openpyxl - load the workbook and save , open saved file with error message

匆匆过客 提交于 2019-12-06 09:46:04
问题 Error Message while opening the file: Excel found unreadable content in zz.xlsx. Do you want to recover the contents of the work book?If you trust the source of the workbook,click Yes. If I say "Yes" got "Repairs to zz.xlxs" pop up shows. Could any one please help me. What format of the excel could have caused this. As i tried sample workbook without any formatting it work fine. My code : from openpyxl import Workbook from openpyxl import load_workbook #open existing workbook wb = load

In openpyxl, how to move or copy a cell range with formatting, merged cells, formulas and hyperlinks

两盒软妹~` 提交于 2019-12-06 09:31:33
I am trying to move a cell range inside a worksheet using Python 2.7 + openpyxl. What seemed to be an easy and basic task turned out to be close to impossible. Here is how I'd like it to look in Excel: To make the task easier let's assume I only need to move range from given cell to the end of data (to last row and column). My first idea was easy: for i, column in enumerate(columns): if i >= starting_col: for j, cell in enumerate(column): if j >= starting_row: copy_cell(ws, j+1, i+1, j+1, i+movement) But hey, what how to efficiently and fully realize copy_cell ? By trial and error I managed to

Python library to open existing .xlsx workbook with charts [closed]

放肆的年华 提交于 2019-12-06 09:17:14
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I have to read a certain .xlsx file (file1.xlsx), pull data from another .csv files (file2.csv) and write the processed data into the original file (file1.xlsx) along with some charts. The file has existing charts, I have looked into openpyxl, xlswriter, xlsread, and other python libraries. It seems openpyxl is the only library which allows you to read and write files back with the same library. Others like

Checking for empty cells with OpenPyXl

旧城冷巷雨未停 提交于 2019-12-06 07:19:36
I've poured over numerous versions of this same question, but simply cannot find a working solution for looping through a group of cells and checking for empty ones. Here's my code: wb = openpyxl.Workbook(write_only=False) sheet = wb.get_sheet_by_name('Sheet') for i in range(1, 20): if sheet.cell(row=i, column=1).value == None or 'None': print('Space' + str(i)) sheet.cell(row=i, column=1) = i else: pass wb.save('filename.xlsx') But for some reason it writes to the empty cells AND overwrites the cells which had values written in them. I comment out the loop I find that if I run the code it

Getting AttributeError 'Workbook' object has no attribute 'add_worksheet' - while writing data frame to excel sheet

邮差的信 提交于 2019-12-06 06:34:24
I have following code and trying to write a data frame into an "Existing" worksheet of an Excel file (referred here as test.xlsx). Sheet3 is the targeted sheet, where I want to place the data and I don't want to replace the entire sheet with a new one. df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]}) book = load_workbook('test.xlsx') writer = pd.ExcelWriter('test.xlsx') writer.book = book writer.sheets = dict((ws.title, ws) for ws in book.worksheets) # *I am not sure what is happening in this line* df.to_excel(writer,"Sheet3",startcol=0, startrow=20) When I am running the code line by

Python openpyxl write list to Excel error

扶醉桌前 提交于 2019-12-06 05:56:01
问题 I am new to Python and working on my first project. I am trying to get my code to copy columns of data from one spreadsheet and append it to data that currently exists in a master sheet. I am able to capture the data in each of the sheets and create a new master list which combines both data sets, but I am having trouble writing it to a file. When I test print the combined lists they appear correct, but when I add the code to write the lists to a file it gets hung up. Any assistance you can

Openpyxl.utils.exceptions.IllegalcharacterError

久未见 提交于 2019-12-06 05:21:27
I have the following code to write processed words into excel file. The words are about 7729 From openpyxl import * book=Workbook () sheet=book.active sheet.title="test" for x in range (7729): sheet.cell (row=1,column=x+1).value=x book.save ('test.xlsx') This is the code I used looks like. But when I run it it gives me an error that says openpyxl.utils.exceptions.IllegalCharacterError This is my first time using this module, I would appreciate any kind of help. Try this : This code works for me . from openpyxl import * book=Workbook () sheet=book.active sheet.title="test" x = 0 with open("temp

csv & xlsx files import to pandas data frame: speed issue

眉间皱痕 提交于 2019-12-06 03:13:34
问题 Reading data (just 20000 numbers) from a xlsx file takes forever: import pandas as pd xlsxfile = pd.ExcelFile("myfile.xlsx") data = xlsxfile.parse('Sheet1', index_col = None, header = None) takes about 9 seconds. If I save the same file in csv format it takes ~25ms: import pandas as pd csvfile = "myfile.csv" data = pd.read_csv(csvfile, index_col = None, header = None) Is this an issue of openpyxl or am I missing something? Are there any alternatives? 回答1: xlrd has support for .xlsx files, and

Applying Format to Entire Row Openpyxl

醉酒当歌 提交于 2019-12-06 02:31:29
问题 I have an Excel File that I want to format. The first row (excluding Headers so row2) should be red and italicized . the Openpyxl Documentation states: If you want to apply styles to entire rows and columns then you must apply the style to each cell yourself I personally thinks this stinks... Here is my workaround: import openpyxl from openpyxl.styles import NamedStyle from openpyxl import load_workbook from openpyxl.styles.colors import RED from openpyxl.styles import Font # I normally

Manipulate existing excel table using openpyxl

岁酱吖の 提交于 2019-12-06 01:41:26
I'm currently honing my python / excel skills, and have run into an issue with openpyxl I am trying to open a workbook, replace rows in an existing table, and save the workbook again. Ideally I'd like to also first be able delete all rows from the table (Though retaining the table structure) My initial workbook contains a sheet named "inputData". In this I have a table named "Data" columns A, B, C and 2 rows of data. I also have a csv file named "input.csv" containing The same columns but 4 rows of data. when I run my code, the data is written into the worksheet, but the table structure is not