insert text file into existing xlsx using Python - python

I have a .xlsx file with 11 worksheets and I need to insert the contents of a text file (tab delim, roughly 30 columns with 100 rows) from Row 3 onwards. I tried the code below but I end up with errors. (using bash/Linux)
#!/usr/bin/env python
import csv
from openpyxl.reader.excel import load_workbook
from xlrd import open_workbook
from xlutils import copy as xl_copy
with open('S12_final.txt') as tab_file: #open tab text file
tab_reader = csv.reader(tab_file, delimiter='\t')
xls_readable_book = load_workbook('S12.xlsx') #load workbook
xls_writeable_book = xl_copy.copy(xls_readable_book)
xls_writeable_sheet = xls_writeable_book.get_sheet_by_name('Filtered') #write data on this sheet
for row_index, row in enumerate(tab_reader):
xls_writeable_sheet.write(row_index, 0, row[0])
xls_writeable_sheet.write(row_index, 1, row[1])
xls_writeable_book.save('S12.xlsx') #save excel file
Errors:
> Traceback (most recent call last): File "./tab2excel_a.py", line 23,
> in <module>
> xls_writeable_book = xl_copy.copy(xls_readable_book) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/copy.py",
> line 19, in copy
> w File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 937, in process
> reader(chain[0]) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 61, in __call__
> filter.workbook(workbook,filename) File "/usr/local/lib/python2.7/dist-packages/xlutils-1.6.0-py2.7.egg/xlutils/filter.py",
> line 287, in workbook
> self.wtbook.dates_1904 = rdbook.datemode AttributeError: 'Workbook' object has no attribute 'datemode'
Any suggestions ?

It seems (and by no means I'm knowledgeable about those particular libraries) that you are attempting to input an openpyxl object into an xlutils method in these lines:
xls_readable_book = load_workbook('S12.xlsx') #load workbook
xls_writeable_book = xl_copy.copy(xls_readable_book)
So the interpreter complains that this "unknown" object has no attribute datemode. As so try using xlrd open_workbook method since it seems to return a Book object which, according to the documentation, is fully compatible with xlrd.copy methods:
xls_readable_book = open_workbook('S12.xlsx') #load workbook
xls_writeable_book = xl_copy.copy(xls_readable_book)

Consider this openpyxl example:
from openpyxl.workbook.workbook import Workbook # as _Workbook
import csv
wb = Workbook()
wb.create_sheet('Filtered')
ws = wb['Filtered']
with open('test/S12_final.csv') as tab_file:
tab_reader = csv.reader(tab_file, delimiter='\t')
# Skipt first 2 Lines
[next(tab_reader) for skip in range(2)]
# Append list(rowData) after Sheets last accessed Row
for rowData in tab_reader:
ws.append(rowData)
wb.save('test/S12.xlsx')
Tested with Python: 3.4.2 - openpyxl: 2.4.1 - LibreOffice: 4.3.3.2

Related

Python openpyxl error: BadZipFile: File is not a zip file

Facing the error "BadZipFile: File is not a zip file" when loading excel workbook using openpyxl load_workbook function. How do I solve this error?
workbook = r'C:\Desktop\Test.xlsx'
worksheet = 'Data'
# create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter(workbook, engine='openpyxl')
wb = load_workbook(workbook)
writer.book = workbook
writer.sheets = {x.title: x for x in wb.worksheets}
ws = writer.sheets[worksheet]
for i in range(len(vehicle_sales)):
row = list(vehicle_sales.iloc[i])
for j in range(len(vehicle_sales.columns)):
value = row[j]
ws.cell(i+2, j+1, value)
xl.writer.excel.save_workbook(wb, workbook)
The excel files were in read-only mode. I saved the file as a new file and load_workbook worked.

Copy Complete Excel row with values, formatting, formulas etc

Well I've a row which i want to copy complete.
I dont know how i can do that correctly in python.
i try to copy the code from another stackoverflow page. But if i test the code i get errors
my own code is this.
# Copy input to output
output_table = input_table.copy()
# conda install -c anaconda openpyxl
import os
import openpyxl
from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
from copy import copy
from copy import deepcopy
from openpyxl.styles import Font, colors, Color, PatternFill, Border, Side, Alignment, Protection
def move_cell(source_cell, dest_row, dest_col, preserve_original=False):
#param source_cell: cell to be moved to new coordinates
#param dest_row: 1-indexed destination row
#param dest_col: 1-indexed destination column
#param preserve_original: if True, does a copy instead of a move
if preserve_original:
cell_to_move = copy.copy(source_cell)
else:
cell_to_move = source_cell
worksheet = cell_to_move.parent
source_address = (cell_to_move.row, cell_to_move.col_idx)
dest_address = (dest_row, dest_col)
cell_to_move.row = dest_row
cell_to_move.col_idx = dest_col
worksheet._cells[dest_address] = cell_to_move
if not preserve_original:
del worksheet._cells[source_address]
return cell_to_move
var_data_path = "C:/"
#var_path_excel_file = var_data_path + os.path.sep + "data.xlsx"
wb = load_workbook(var_path_excel_file)
ws = wb["test"]
# activate the ws 'data'
for s in range(len(wb.sheetnames)):
if wb.sheetnames[s] == ws:
break
wb.active = s
mr = ws.max_row
mc = ws.max_column
# copying the cell values from source
# excel file to destination excel file
for j in range (1, mc + 1):
c = ws.cell(row = 10, column = j)
#ws.cells is my idea. But it don't copy the style and dont manipulate the formulas. i.e. =D10 to =D12 . Dependent from the row. Move cell function is the function which i copied from another great user. But i get errors for that. i test this all with the row 10. I want to copy the row 10 to 15. My error is function' object has no attribute 'copy' Traceback (most recent call #last): File "<string>", line 85, in <module>
# File "<string>", line 19, in move_cell
#AttributeError: 'function' object has no attribute 'copy'
ws.cell(15, column = j).value = c.value
move_cell(c, 15, j, preserve_original=True)
#ws[15] = ws[10]
wb.save(var_path_excel_file)
In openpyxl, how to move or copy a cell range with formatting, merged cells, formulas and hyperlinks

Trying to convert excel sheets to PDF using Python but throwing up this error

Hi I am trying to convert excel sheet to pdf using python, converted a script wrote to do same with word documents, which works fine but having this error below flagging up
Traceback (most recent call last):
File "C:/Users/alank/Python training/Exceltopdf2.py", line 13, in <module>
xlxs.SaveAs(out_file, FileFormat=xlxsFormatPDF)
OSError: exception: access violation reading 0xFFFFFFFFFFFFFFFF
any help appreciated and script is below
import sys
import os
import comtypes.client
xlxsFormatPDF = 17
in_file = (r'C:\Users\alank\Python training\Helloworld.xlsx')
out_file = (r'C:\Users\alank\Python training\Helloworld.pdf')
excel = comtypes.client.CreateObject('Excel.Application')
xlxs = excel.workbooks.Open(in_file)
xlxs.SaveAs(out_file, FileFormat=xlxsFormatPDF)
xlxs.Close()
excel.Quit()
You can try with win32com.client
like this:
import win32com.client
from pywintypes import com_error
WB_PATH = r'C:\Users\alank\Python training\Helloworld.xlsx'
PATH_TO_PDF = r'C:\Users\alank\Python training\Helloworld.pdf'
excel.Visible = False
try:
# Open
wb = excel.Workbooks.Open(WB_PATH)
# Specify the sheet you want to save by index.
#if you want all the sheets in excel try with:
#wb.WorkSheets(wb.Sheets.Count) or wb.WorkSheets([i=1 for i in range(wb.Sheets.Count)]).Select()
ws_index_list = [1,2,3,4,5,6,7,8,9,10,11,12]
wb.WorkSheets(ws_index_list).Select()
# Save
wb.ActiveSheet.ExportAsFixedFormat(0, PATH_TO_PDF)
except com_error as e:
print('The convertion failed.')
else:
print('Succeessful convertion')
finally:
wb.Close()
excel.Quit()
Or you can do it like here (Andreas solution):
import os
import comtypes.client
SOURCE_DIR = r'C:\Users\alank\Python training'
TARGET_DIR = r'C:\Users\alank\Python training'
app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False
infile = os.path.join(os.path.abspath(SOURCE_DIR), 'Helloworld.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'Helloworld.pdf')
doc = app.Workbooks.Open(infile)
doc.ExportAsFixedFormat(0, outfile, 1, 0)
doc.Close()
app.Quit()

openpyxl copy from one workbook to another

I am trying to copy the cells from one workbook to another, to do some manipulation. While i am able to assign style/value and assign it to new workbook i am unable to assign the font,fill from the existing workbook to new workbook.
Below is the snippet of my code
from openpyxl import load_workbook
from openpyxl import Workbook
File = load_workbook(filename='testcopy.xlsx')
FileSheets = File.get_sheet_names()
AcSheet = File.active
write_file = Workbook()
wr_ac_sheet = write_file.active
wr_ac_sheet['A1'].value = AcSheet['A1'].value
wr_ac_sheet['A1'].style = AcSheet['A1'].style
write_file.save('copied_excel.xlsx')
This works fine, but if i use
wr_ac_sheet['A1'].font= AcSheet['A1'].font
i get the below error
File "C:\Python27\lib\site-packages\openpyxl\writer\excel.py", line 90, in write_data
stylesheet = write_stylesheet(self.workbook)
File "C:\Python27\lib\site-packages\openpyxl\styles\stylesheet.py", line 206, in write_stylesheet
stylesheet.fonts = wb._fonts
File "C:\Python27\lib\site-packages\openpyxl\descriptors\sequence.py", line 27, in __set__
seq = [_convert(self.expected_type, value) for value in seq]
File "C:\Python27\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
raise TypeError('expected ' + str(expected_type))
TypeError: expected <class 'openpyxl.styles.fonts.Font'>
If i try to print the existing workbook font, i get this, so it's reading properly
print wr_ac_sheet['A1'].font
<openpyxl.styles.fonts.Font object>
Parameters:
name='Calibri', charset=None, family=2.0, b=False, i=False, strike=None,
outline=None, shadow=None, condense=None, color=
<openpyxl.styles.colors.Color
object>
Parameters:
tint=0.0, auto=None, theme=1L, rgb=None, indexed=None, type='theme',
extend=None, sz=11.0, u=None, vertAlign=None, scheme='minor'
i would like to assign all the properties of the cell (similar to format painter) to the new workbook, any guidance on how to do that?
As explained in the documentation you need to copy the style information.
Why do you set the explicit style?
#!/usr/bin/python3
# -*- coding: utf8 -*-
import sys
import openpyxl
import warnings
warnings.simplefilter("ignore")
xlsxfile = sys.argv[1]
xlscopy = sys.argv[2]
wb = openpyxl.load_workbook(xlsxfile)
sheet = wb['Sheet1']
val = sheet['J7'] # set up an initial value, eg. 34
val.value = 2
wb.save(xlscopy)
This works for me for big and complex formatted XLSX files.

Populate Excel from Python list

I want to populate a excel sheet with data read from a text file.
My script opens a text file and then puts certain attributes into a list. I then want to populate my excel sheet with data in the list.
I have many txt documents in the same location so my script loops though the files.
What is wrong with my code ? It only populates one row.
import xlwt
import os
output = 'D:\Holding_Area\\test.xls'
file_path='Y:\\Testing\\Crashes'
pathappend=[]
r=1
for a in os.listdir(file_path):
pathappend.append(file_path+'\\'+a)
def main():
for x in pathappend:
appendlist=[]
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python', cell_overwrite_ok=True)
file = open(x)
lines = file.readlines()
appendlist.append(lines[1][2:10])
appendlist.append(lines[1][13:21])
appendlist.append(lines[4][15:30])
appendlist.append(lines[10][13:22])
appendlist.append(lines[11][9:28])
appendlist.append(lines[22])
appendlist.append(lines[31][84:113])
appendlist.append(lines[27:29])
file.close()
for i,e in enumerate(appendlist):
sheet.write(r,i,e)
r+1
wbk.save(output)
main()
Issue was with the 'r+1' it should be 'r+=1'; as shown below:
import xlwt
import os
output = 'D:\Holding_Area\\test.xls'
file_path='Y:\\Testing\\Crashes'
pathappend=[]
r=1
for a in os.listdir(file_path):
pathappend.append(file_path+'\\'+a)
def main():
for x in pathappend:
appendlist=[]
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('python', cell_overwrite_ok=True)
file = open(x)
lines = file.readlines()
appendlist.append(lines[1][2:10])
appendlist.append(lines[1][13:21])
appendlist.append(lines[4][15:30])
appendlist.append(lines[10][13:22])
appendlist.append(lines[11][9:28])
appendlist.append(lines[22])
appendlist.append(lines[31][84:113])
appendlist.append(lines[27:29])
file.close()
for i,e in enumerate(appendlist):
sheet.write(r,i,e)
r+=1
wbk.save(output)
main()

Categories

Resources